在项目中找不到模块(Python)

时间:2020-10-07 12:23:43

标签: python module pytest

我是一个初学者,我不了解项目文件夹结构中的某些内容。

我有这个项目:

.
└── convertersProject/
    ├── conftest.py -----------------> best practice if you're not using src structure
    ├── converters  -----------------> module I'm trying to import/
    │   ├── __init__.py
    │   ├── binconverter.py
    │   └── importester1.py  --------> import binconverter OR from converters import */
    │       └── submodule/
    │           ├── __init__.py -----> not needed if I don't want to use it as package
    │           └── importester2.py -> from converter import binconverter 
    ├── outsidemodule  /
    │   ├── importester3.py
    │   └── __init__.py 
    └── test /
        └── test_converters.py

尝试直接从项目文件夹执行ModuleNotErrorFound时,我总是收到importester1/2/3.py。我使用的是虚拟环境,是从pyenv python 3.8.5中将python -m venv 'name'设置为pyenv shell 3.8.5

我想我了解的是

  • 我必须使用绝对路径,因为from converters.binconverter import bin2dec是binconverter中的bin2dec功能。如果我想使用相对的,我应该像尝试执行importertest2.py一样在文件夹树中,因为submoduleconverters内部。因此,我不应将亲戚用作outsidemodule
  • PYTHONPATH是您正在执行的当前文件夹,因此,如果我从项目文件夹中以python converters/submodule/importester2.py的身份执行,则不必在虚拟环境的PYTHONPATH后面附加任何值(实际上,我已经阅读了这不是一个好习惯)
  • __init__.py允许您使用模块而无需向PYTHONPATH virtualenv附加值,因此我可以使用绝对路径将import模块converter插入outsidemodule
  • tests正在使用此逻辑。实际上,如果我更改某些内容,VSCode调试器将检测到import问题。

我到底想念什么?

0 个答案:

没有答案