【发布时间】:2021-05-09 10:33:39
【问题描述】:
我的系统似乎无法从其他文件夹访问模块。例如,如果我将我的目录设置为
~/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 ======================================================
【问题讨论】:
-
如何理解“从其他文件夹访问模块”?文件“test_fibonacci.py”存储在“面向对象编程\测试”中。终端运行时需要文件的路径才能找到文件。
-
已解决:需要使用 python -m pip install -e 安装软件包
标签: python visual-studio-code importerror modulenotfounderror