运行 python 来自命令行,只有项目导入失败

Running python from command line, only project imports are failing

我正在尝试从命令行而不是 IDE 运行 我的 python 代码。我从 git 克隆了我的项目,并创建了一个 python 3 虚拟环境。我已经激活了我的 venv 和 python --version 之类的命令在我的 venv 中正确打印了 python 版本(与我将在 venv 之外获得 运行ning 相同命令的版本相对,这是不同的在我的例子中)所以我知道我正在正确使用 venv。进入激活的 venv 后,我使用 pip pip install -r requirements.txt 从 requirments.txt 文件中 pip 安装我的第 3 方软件包,但我仍然无法 运行 安装我的代码。这是我的目录布局:

project_folder
├── env_vars
|   ├── __init__.py
|   └── env_vars.py
|
├── tests
|   ├── __init__.py
|   └── test.py
|
└── __init__.py

我正在尝试 运行 test.py 其导入看起来像:

import os  # python built in, gets past this line no problem
from 3rd_praty_lib import 3rd_party_thing  # this is a library I installed with pip, again gets past this line no problem
from env_vars import env_vars  # <- this is where the failure happens. referencing my own code

基本上我的问题是:

在测试目录中使用命令 python test.py 时出现此错误:

 File "test.py", line 3, in <module>
    from env_vars import env_vars
ModuleNotFoundError: No module named 'env_vars'

你应该把你的包放到 python 路径

import sys
sys.path.insert(0, "/path/to/your/package_or_module")