无法导入模块python ImportError

时间:2016-04-02 12:58:58

标签: python importerror flask-restful

我有一个具有此结构的项目(仅显示受影响的人):

main.py # starting script
/app
  |__init__.py
  |/exceptions
    | __init__.py # content: from .ex400 import *
    | base.py
    | ex400.py # content: some classes extending classes from base.py
  |/extend
    | __init__.py # empty
    | module.py

来自/app/extend/module.py,我尝试从/app/exceptions/ex400.py导入类。从主要我导入module.py

module.py

# I added this to check the path, output is below
import sys
print 'Working directory =>', sys.path

from app.exceptions.ex400 import SomeClass, MoreClass

# ... some code here ofcourse

跑步时,我得到ImportError

Working directory => ['/prj/myproject', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']

...
File "/prj/myproject/app/extend/module.py", line 5, in <module>
   from app.exceptions.ex400 import SomeClass, MoreClass
ImportError: No module named exceptions.ex400

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:2)

是的,你错过了一些东西。它只是一些东西,但用Python导入是一个痛苦的** **。例如,您 app - 文件夹中没有__init__.py。那应该可以解决你的问题。顺便说一句,不要混淆名称空间等。我只是将例外 - 文件夹中的__init__.py留空。

干杯