2017-04-19 75 views
0

Error: ImportError: No module named common_components.modules.logging.logger运行时发生python emailsender.pyPython:导入错误我自己的模块

输出:

(dev) [email protected]:~/workspace/python/python/common_components/modules/email$ python emailsender.py 
Traceback (most recent call last): 
    File "emailsender.py", line 23, in <module> 
    from common_components.modules.logging.logger import Log 
ImportError: No module named common_components.modules.logging.logger 

不知道是否有毛病以下项目结构:

(dev) [email protected]:~/workspace/python/python$ tree 
. 
├── common_components 
│   ├── __init__.py 
│   ├── modules 
│   │   ├── email 
│   │   │   ├── emailsender.py 
│   │   │   └── __init__.py 
│   │   ├── __init__.py 
│   │   ├── logging 
│   │   │   ├── __init__.py 
│   │   │   └── logger.py 
└── __init__.py 

任何想法?由于

UPDATE

(dev) [email protected]:~/workspace/python$ python python/common_components/modules/email/emailsender.py 
Traceback (most recent call last): 
    File "python/common_components/modules/email/emailsender.py", line 23, in <module> 
    from common_components.modules.logging.logger import Log 
ImportError: No module named common_components.modules.logging.logger 
(dev) [email protected]:~/workspace/python$ cd python/ 
(dev) [email protected]:~/workspace/python/python$ python common_components/modules/email/emailsender.py 
Traceback (most recent call last): 
    File "common_components/modules/email/emailsender.py", line 23, in <module> 
    from common_components.modules.logging.logger import Log 
ImportError: No module named common_components.modules.logging.logger 
+1

'common_components'是不是在你的Python路径。解决这个问题最简单的方法就是用''common_components/modules/email/emailsender.py''来运行你的脚本。工作目录总是在python路径中。 –

+0

你的'python'目录里有'__init __。py'。不知道这是否有所作为,但它确实看起来很奇怪,因为我认为你不打算成为一个包装。 –

回答

0

的Python将当前目录到PYTHONPATH,但你的当前目录是~/workspace/python/python/common_components/modules/email

修复此通过从~/workspace/python/python/运行:

[email protected]:~/workspace/python/python$ python common_components/modules/email/emailsender.py 

或者通过手动设置PYTHONPATH

[email protected]:~/$ set PYTHONPATH=~/workspace/python/python/ 

[email protected]:~/workspace/python/python/common_components/modules/email$ python emailsender.py 
+0

谢谢。但不工作。我的帖子已更新。谢谢 – BAE

+0

您需要从'workspace/python/python'目录执行,以便找到'common_components'包。你正在执行'workspace/python',它有'python'目录,而不是'common_components'。 –

+0

试过了。不工作。有线。 – BAE