【问题标题】:ImportErrors and ModuleNotFoundError for pythonpython 的 ImportErrors 和 ModuleNotFoundError
【发布时间】: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 ====================================================== 

Error Output

【问题讨论】:

  • 如何理解“从其他文件夹访问模块”?文件“test_fibonacci.py”存储在“面向对象编程\测试”中。终端运行时需要文件的路径才能找到文件。
  • 已解决:需要使用 python -m pip install -e 安装软件包

标签: python visual-studio-code importerror modulenotfounderror


【解决方案1】:

在处理“import”子句时,Python会首先检查它是否在其安装文件夹中或是否安装了包,如果失败则搜索当前工作目录,并考虑相对路径。

例如import tests.test_fibonacci表示在你当前的工作目录中有一个名为tests的文件夹,脚本test_fibonacci.py在里面,python会从命名空间“tests”的第一行到最后一行运行它.test_fibonacci"。

【讨论】:

  • 那我应该如何设置我的工作目录呢?选择一个会给我一个 ImportError,另一个给我一个 ModuleNotFoundError。
  • 我建议将项目需要导入的所有python脚本复制到一个目录中。
  • 这可行,但有没有办法让导入真正起作用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-22
  • 2020-06-14
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多