2017-04-19 32 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