简体   繁体   English

如何解决“ ImportError:没有名为…的模块”

[英]How to fix “ImportError: No module named …”

I have reviewed most of the similar question here. 我已经在这里复查了大多数类似的问题。 I'm new to python and I'm using Ubuntu 13.10 The project structure is 我是python的新手,正在使用Ubuntu 13.10。项目结构为

├── projecttest
│   ├── api.py
│   ├── controller
│   │   ├── controller.py
│   │   ├── controller.pyc
│   │   ├── init_db.py
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── settings.py
│   │   ├── settings.pyc
│   │   └── extra
│   │       ├── extra.py
│   │       ├── extra.pyc
│   │       ├── __init__.py
│   │       └── __init__.pyc
│   ├── __init__.py
│   ├── lib
│   │   └── __init__.py
│   ├── models
│   │   ├── documents.py
│   │   ├── documents.pyc
│   │   └── __init__.py

All the __init__.py files are empty (no hidden characters) and when I'm trying 当我尝试时,所有__init__.py文件都是空的(没有隐藏的字符)

$ python init_db.py

that has: 具有:

from projecttest.models.documents import *

I'm getting: 我越来越:

Traceback (most recent call last):
  File "controllers/init_db.py", line 1, in <module>
    from projecttest.models.documents import *
ImportError: No module named projecttest.models.documents

You need to specify PYTHONPATH environment variable, it augments the default search path for module files. 您需要指定PYTHONPATH环境变量,它会增加模块文件的默认搜索路径。

It helps to think about PYTHONPATH as an absolute path. 可以将PYTHONPATH视为绝对路径。 If you specify it you may import modules within your program relative to PYTHONPATH. 如果指定它,则可以在程序中相对于PYTHONPATH导入模块。

In your case it would be something like the following line: 在您的情况下,将类似于以下行:

PYTHONPATH=/<dir>/<folder>/projecttest/ python init_db.py

Then you may import modules without problems like: 然后,您可以导入模块而不会出现以下问题:

from models.documents import *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM