如何解决Python 的 ImportErrors 和 ModuleNotFoundError
我的系统似乎无法访问其他文件夹中的模块。例如,如果我将目录设置为
http {
...
default_type application/json;
...
}
然后运行
Content-Type: application/json
我明白
~/Downloads/principles_of_programming/object-oriented-programming/fibonacci
如果我将目录更改为
pytest tests/test_fibonacci.py
并按照上面的方式运行测试,我得到
==================================================== test session starts =====================================================
platform win32 -- Python 3.9.1,pytest-6.2.2,py-1.10.0,pluggy-0.13.1
rootdir: C:\Users\Ryan\Downloads\principles_of_programming\object-oriented-programming,configfile: setup.cfg
collected 0 items
=================================================== no tests ran in 0.05s ====================================================
ERROR: file or directory not found: tests/test_fibonacci.py
解决方法
处理“import”子句时,Python会首先检查它是否在它的安装文件夹中或是否有安装包,如果失败,则搜索当前工作目录,并考虑相对路径。
例如import tests.test_fibonacci
表示在你当前的工作目录中有一个名为tests
的文件夹,脚本test_fibonacci.py
在里面,python会从第一行运行到最后一行命名空间“tests.test_fibonacci”中的行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。