summary:python_problem

本文档解答了Python编程中常见的几个问题,包括numpy文件加载错误、构造函数定义、使用pip安装及管理Python包、Ubuntu环境下运行Python脚本、MNIST数据集加载失败、类与对象的基本概念以及TensorFlow中Session使用的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.need more than 1 value to unpack:

solution:[.npy] need use [unmpy.load]

2.TypeError: this constructor takes no arguments:
Solution:在python中构造函数书写格式是__init__,而不是_init_,即在init两侧都是双下划线,不是单下划线。

3.安裝:pip install PackageName

更新:pip install -U PackageName

移除:pip uninstall PackageName

搜索:pip search PackageName

帮助:pip help

4.ubuntu中运行python脚本
https://www.cnblogs.com/hester/p/5575658.html

5.MNIST_load

IOError: [Errno socket error] [Errno 101] Network is unreachable

手工下载数据包,将下载的4个数据拷贝到当前目录,read_data_sets 会先判断本地是否存在数据包。


6.__int__,class,self:

https://www.crifan.com/summary_the_basic_knowledge_about_object_oriented/

https://www.crifan.com/summary_the_meaning_of_self_and___init___in_python_and_why_need_them/

5.RuntimeError: Attempted to use a closed Session.

https://blog.youkuaiyun.com/weixin_39390732/article/details/76474294

6.append():

http://www.runoob.com/python/att-list-append.html

``` from flask import Flask,render_template,request,redirect,url_for,session import unittest app=Flask(__name__) #主页 @app.route('/') def index(): if "username" in session: return redirect(url_for("success",name = session["username"])) return redirect(url_for("login")) #登录成功页面 @app.route('/success/<name>') def success(name): return f"{name} login success!" @app.route('/login',methods = ["GET","POST"]) def login(): if request.method == "POST": session["username"] = request.form.get("username") return redirect(url_for("success",name = session["username"])) return render_template('login1.html') if __name__ == '__main__': app.run(debug=True)```E:\202312100217-Ljw\flask_web\envs\python.exe "D:/Program Files/JetBrains/PyCharm 2023.1/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --target get_post.py::test Testing started at 8:29 ... D:/Program Files/JetBrains/PyCharm 2023.1/plugins/python/helpers/pycharm/_jb_pytest_runner.py:8: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html from pkg_resources import iter_entry_points Launching pytest with arguments get_post.py::test --no-header --no-summary -q in E:\202312100217-Ljw\路由 ============================= test session starts ============================= collecting ... collected 1 item get_post.py::test FAILED [100%] get_post.py:3 (test) @app.route('/',methods = ['GET','POST']) def test(): > if request.method == 'GET': get_post.py:6: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ..\flask_web\envs\lib\site-packages\werkzeug\local.py:432: in __get__ obj = instance._get_current_object() ..\flask_web\envs\lib\site-packages\werkzeug\local.py:554: in _get_current_object return self.__local() # type: ignore _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ name = 'request' def _lookup_req_object(name): top = _request_ctx_stack.top if top is None: > raise RuntimeError(_request_ctx_err_msg) E RuntimeError: Working outside of request context. E E This typically means that you attempted to use functionality that needed E an active HTTP request. Consult the documentation on testing for E information about how to avoid this problem. ..\flask_web\envs\lib\site-packages\flask\globals.py:33: RuntimeError ============================== 1 failed in 0.24s ============================== Process finished with exit code 1
03-20
========================================================================================================== test session starts =========================================================================================================== platform win32 -- Python 3.7.3, pytest-7.4.4, pluggy-1.2.0 -- d:\python\python.exe cachedir: .pytest_cache metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.22621-SP0', 'Packages': {'pytest': '7.4.4', 'pluggy': '1.2.0'}, 'Plugins': {'allure-pytest': '2.14.3', 'html': '3.2.0', 'metadata': '3.0.0', 'ordering': '0.6', 'rerunfailures': '13.0'}} rootdir: D:\pythonProject\接口自动化练习 configfile: pytest.ini testpaths: ./test_moudle plugins: allure-pytest-2.14.3, html-3.2.0, metadata-3.0.0, ordering-0.6, rerunfailures-13.0 collecting ... 测试init方法会调用多少次 collected 3 items test_moudle/test_login.py::Test_Home::test_home[Baseinfo] 2025-07-12 21:21:09,686 INFO [root] [test_login.py(test_home:14)] - 开始执行测试用例:test_api_example FAILED test_moudle/test_login.py::Test_Home::test_home[testcase] 2025-07-12 21:21:09,740 INFO [root] [test_login.py(test_home:14)] - 开始执行测试用例:test_api_example FAILED test_moudle/test_employee.py::TestEmployee::test_add_employee[case0] FAILED ================================================================================================================ FAILURES ================================================================================================================ _____________________________________________________________________________________________________ Test_Home.test_home[Baseinfo] ______________________________________________________________________________________________________ self = <test_moudle.test_login.Test_Home object at 0x00000186D452F780>, case = 'Baseinfo' @pytest.mark.parametrize("case", read_yaml("./test_moudle/login.yaml")) @allure.epic("苍穹外卖") @allure.story("登录模块") @allure.severity(allure.severity_level.CRITICAL) def test_home(self, case): logging.info("开始执行测试用例:test_api_example") > allure.dynamic.title(case["testcase"]["title"]) E TypeError: string indices must be integers test_moudle\test_login.py:16: TypeError ----------------------------------------------------------------------------------------------------------- Captured log call ------------------------------------------------------------------------------------------------------------ INFO root:test_login.py:14 开始执行测试用例:test_api_example _____________________________________________________________________________________________________ Test_Home.test_home[testcase] ______________________________________________________________________________________________________ self = <test_moudle.test_login.Test_Home object at 0x00000186D452F9E8>, case = 'testcase' @pytest.mark.parametrize("case", read_yaml("./test_moudle/login.yaml")) @allure.epic("苍穹外卖") @allure.story("登录模块") @allure.severity(allure.severity_level.CRITICAL) def test_home(self, case): logging.info("开始执行测试用例:test_api_example") > allure.dynamic.title(case["testcase"]["title"]) E TypeError: string indices must be integers test_moudle\test_login.py:16: TypeError ----------------------------------------------------------------------------------------------------------- Captured log call ------------------------------------------------------------------------------------------------------------ INFO root:test_login.py:14 开始执行测试用例:test_api_example _________________________________________________________________________________________________ TestEmployee.test_add_employee[case0] __________________________________________________________________________________________________ self = <test_moudle.test_employee.TestEmployee object at 0x00000186D451BDD8> case = {'request': {'data': {'id': 5, 'idNumber': 432502200112033212, 'name': '小样', 'phone': 18707369896, ...}, 'headers': {'...': 'application/json', 'token': ''}, 'method': 'post', 'url': 'http://localhost:8080/admin/employee'}, 't itle': '增加员工'} @pytest.mark.parametrize("case",read_yaml("./test_moudle/employee.yaml")) def test_add_employee(self,case): method=case["request"]["method"] url=case["request"]["url"] data=case["request"]["data"] headers=case["request"]["headers"] > headers["token"]=read_token("token") test_moudle\test_employee.py:16: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ key = 'token' def read_token(key): with open("token.yaml",'r',encoding='UTF-8') as f: value=yaml.safe_load(f) > return value[key] E TypeError: 'NoneType' object is not subscriptable yaml_untils.py:15: TypeError ======================================================================================================== short test summary info ========================================================================================================= FAILED test_moudle/test_login.py::Test_Home::test_home[Baseinfo] - TypeError: string indices must be integers FAILED test_moudle/test_login.py::Test_Home::test_home[testcase] - TypeError: string indices must be integers FAILED test_moudle/test_employee.py::TestEmployee::test_add_employee[case0] - TypeError: 'NoneType' object is not subscriptable =========================================================================================================== 3 failed in 0.22s ============================================================================================================ D:\pythonProject\接口自动化练习>Traceback (most recent call last): 'Traceback' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> File "D:/pythonProject/接口自动化练习/yaml_untils.py", line 18, in <module> 命令语法不正确。 D:\pythonProject\接口自动化练习> print(read_yaml("./test_moudle/login.yaml")) 无法初始化设备 PRN D:\pythonProject\接口自动化练习> File "D:/pythonProject/接口自动化练习/yaml_untils.py", line 5, in read_yaml 'File' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> readYaml=yaml.safe_load(f) 'readYaml' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> File "D:\python\lib\site-packages\yaml\__init__.py", line 125, in safe_load 'File' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> return load(stream, SafeLoader) 'return' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> File "D:\python\lib\site-packages\yaml\__init__.py", line 81, in load 'File' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> return loader.get_single_data() 'return' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> File "D:\python\lib\site-packages\yaml\constructor.py", line 49, in get_single_data 'File' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> node = self.get_single_node() 'node' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> File "D:\python\lib\site-packages\yaml\composer.py", line 36, in get_single_node 'File' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> document = self.compose_document() 'document' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> File "D:\python\lib\site-packages\yaml\composer.py", line 55, in compose_document 'File' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> node = self.compose_node(None, None) 'node' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> File "D:\python\lib\site-packages\yaml\composer.py", line 84, in compose_node 'File' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> node = self.compose_mapping_node(anchor) 'node' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> File "D:\python\lib\site-packages\yaml\composer.py", line 127, in compose_mapping_node 'File' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> while not self.check_event(MappingEndEvent): 'while' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> File "D:\python\lib\site-packages\yaml\parser.py", line 98, in check_event 'File' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> self.current_event = self.state() 'self.current_event' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> File "D:\python\lib\site-packages\yaml\parser.py", line 439, in parse_block_mapping_key 'File' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> "expected <block end>, but found %r" % token.id, token.start_mark) '"expected <block end>, but found %r"' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习>yaml.parser.ParserError: while parsing a block mapping 'yaml.parser.ParserError:' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> in "./test_moudle/login.yaml", line 1, column 1 'in' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习>expected <block end>, but found '-' 系统找不到指定的文件。 D:\pythonProject\接口自动化练习> in "./test_moudle/login.yaml", line 12, column 1 'in' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\pythonProject\接口自动化练习> D:\pythonProject\接口自动化练习>Process finished with exit code 1 'Process' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
最新发布
07-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值