【问题标题】:Python: 'ImportError: No Module Named <my module>'Python:'ImportError:没有名为 <我的模块> 的模块'
【发布时间】:2018-05-13 06:13:41
【问题描述】:

我正在使用 PyCharm CE 开发一个类似这样结构的项目:

test_python/
|-- app
|   |-- __init__.py
|   |-- mymodule.py
|   |-- mymodule.pyc
|   `-- test_mymodule.py
|-- config.py
`-- tests
    |-- __init__.py
    |-- test_config.py
    `-- test_models.py

当我尝试运行 test_config.py 等测试脚本时,我得到:

$ python tests/test_config.py 
Traceback (most recent call last):
  File "tests/test_config.py", line 1, in <module>
    from config import app_config
ImportError: No module named config

我已经阅读了很多其他 SO 帖子,这些帖子谈到在所有作为包的目录中都需要一个 init.py 文件(我已经这样做了)。许多人还建议使用sys.path。我对后一种方法的问题是我以前从未干预过这些路径。我不确定我的开发环境设置是否发生了变化,但这里是:

Python 2.7 | macOS 塞拉利昂 | PyCharm CE

我尝试使用 virtualenv 安装虚拟环境,但没有发现差异。这是github 上的示例项目,如果您想自己运行它

【问题讨论】:

标签: python python-2.7 configuration virtualenv setup-project


【解决方案1】:

我认为如果您将运行配置中的工作目录更改为test_python,它将起作用。导入语句中的所有包都与 Python 路径中的某个条目相关,工作目录通常位于 Python 路径中。

【讨论】:

    【解决方案2】:

    文件夹深度似乎有问题。替换from ../config import app_config

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-30
    • 2015-12-09
    • 2012-01-26
    • 2020-12-15
    • 2015-11-17
    • 2016-07-24
    • 2017-07-15
    • 1970-01-01
    相关资源
    最近更新 更多