【发布时间】:2019-04-18 14:25:25
【问题描述】:
我正在尝试从命令行而不是从我的 IDE 运行我的 python 代码。我已经从 git 克隆了我的项目,并制作了一个 python 3 虚拟环境。我已经激活了我的 venv 并且像 python --version 这样的命令在我的 venv 中正确打印了 python 版本(与我将在 venv 之外运行相同命令的版本相对,这在我的情况下是不同的)所以我知道我正在使用 venv对。进入激活的 venv 后,我使用 pip pip install -r requirements.txt 从 requirments.txt 文件安装我的第 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'
【问题讨论】:
-
你把库路径加到pythonpath了吗?
-
什么是pythonpath?
-
是的,那是你的问题 => stackoverflow.com/a/19917565/1695172
-
只是一个简单的猜测:From env_vars.env_vars import env_vars
-
在你的导入之前添加这个:
import sys; sys.path.append('..')
标签: python python-3.x virtualenv