我是一个初学者,我不了解项目文件夹结构中的某些内容。
我有这个项目:
.
└── 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
一样在文件夹树中,因为submodule
在converters
内部。因此,我不应将亲戚用作outsidemodule
python converters/submodule/importester2.py
的身份执行,则不必在虚拟环境的PYTHONPATH后面附加任何值(实际上,我已经阅读了这不是一个好习惯)__init__.py
允许您使用模块而无需向PYTHONPATH virtualenv附加值,因此我可以使用绝对路径将import
模块converter
插入outsidemodule
。tests
正在使用此逻辑。实际上,如果我更改某些内容,VSCode调试器将检测到import
问题。我到底想念什么?