python - 在项目中找不到模块(Python)
问题描述
我是初学者,我不了解项目文件夹结构中的某些内容。
我有这个项目:
.
└── 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
直接从项目文件夹执行时,我总是收到。我正在使用虚拟环境,python -m venv 'name'
从 pyenv python 3.8.5 设置pyenv shell 3.8.5
我想我理解的是:
- 我必须使用绝对路径作为
from converters.binconverter import bin2dec
binconverter 中的 bin2dec 功能。如果我想使用相对,我应该在文件夹树内尝试执行importertest2.py
,因为submodule
在里面converters
。所以,我不能用亲戚outsidemodule
- PYTHONPATH 是您正在执行的当前文件夹,因此如果我从项目文件夹执行为
python converters/submodule/importester2.py
,我不必将任何值附加到虚拟环境的 PYTHONPATH (确实,我读过这不是一个好习惯) __init__.py
允许您在不向 PYTHONPATH virtualenv 附加值的情况下使用模块,因此我可以import
将converter
模块outsidemodule
使用绝对路径。tests
正在使用这个逻辑。事实上,如果我更改某些内容,VSCode 调试器会检测到import
问题。
我到底在想什么?
解决方案
推荐阅读
- python - 如何在 Django 中添加额外的列 mysql 数据库表?
- python - 有没有办法将 selenium 与 chrome 而不是 chromedriver 一起使用?
- mysql - 只是将新数据从 Cloud SQL 导入 BigQuery?
- python - 为什么 pandas 数据框变成了 NoneType 对象?
- azure - 参考 Azure DevOps 上的 MS Access Engine 进行集成测试
- elixir - Elixir Flow 在本地机器上工作,但不处理数据并抛出:[warn] ** Undefined handle_info in "GenStage.Streamer" in AWS Fargate
- c# - 如何在 Unity 中根据混音器的音量设置滑块的音量?
- python-3.x - 用另一个列表拆分一个列表
- karate - 空手道:如何从空手道框架的特征中生成的 URL 中提取变量
- c++ - 获取输入行并将该行中的单词添加到 C++ 中的向量的最佳方法是什么?