我的系统似乎无法从其他文件夹访问模块。例如,如果我将我的目录设置为
~/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
如果我将目录更改为
~/Downloads/principles_of_programming/object-oriented-programming
然后按照上面的方法运行测试
==================================================== 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 / 1 error
=========================================================== ERRORS ===========================================================
__________________________________________ ERROR collecting tests/test_fibonacci.py __________________________________________
ImportError while importing test module 'C:\Users\Ryan\Downloads\principles_of_programming\object-oriented-programming\tests\test_fibonacci.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
..\..\..\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests\test_fibonacci.py:1: in <module>
from fibonacci import fib
E ModuleNotFoundError: No module named 'fibonacci'
================================================== short test summary info ===================================================
ERROR tests/test_fibonacci.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
====================================================== 1 error in 0.45s ======================================================
在处理“import”子句时,Python将首先检查它是否在安装文件夹中,或者是否安装了包,如果失败,则搜索当前工作目录,并考虑相对路径
例如
import tests.test_fibonacci
表示当前工作目录中有一个名为tests
的文件夹,脚本test_fibonacci.py
在其中,python将从名称空间“tests.test\u fibonacci”的第一行到最后一行运行它相关问题 更多 >
编程相关推荐