2016-04-18 18 views
0

我遇到了一個與this one非常相似的問題。我也是從編程Python第4版。我正在使用一個mac powerbook。我有類似的目錄結構:模塊搜索路徑沒有通過添加__init__.py和編輯PYTHONPATH修復

programming\ python 
├── PP4E 
│   ├── GUI 
│   │   ├── Tour 
│   │   │   ├── __init__.py 
│   │   │   ├── demoAll-prg.py 
│   │   │   ├── demoAll-win.py 
│   │   │   ├── demoCheck.py 
│   │   │   ├── demoDlg.py 
│   │   │   ├── demoRadio.py 
│   │   │   ├── demoScale.py 
│   │   ├── __init__.py 
│   ├── __init__.py 
│   └── launchmodes.py 
└── __init__.py 

的demoAll-prg.py試圖from PP4E.launchmodes import PortableLauncher,但返回ImportError: No module named 'PP4E'

關於SO的回答通常會說確保文件夾中存在__init__.py文件(完成!),和/或將父目錄(這裏稱爲「編程python」)添加到PYTHONPATH中。這也同意學習Python,第5版。,p。 709.

我試圖編輯我的Python路徑,例如描述here,無濟於事的幾個不同的方法。目前我的.bash_profile已將此添加到最後:

PYTHONPATH=「/Users/geoffreysametz/google drive/programming python」 
export PYTHONPATH 

但這並不是解決問題。我已經關閉並重新打開終端,並重新啓動了電腦。

有關如何解決此問題的任何建議,我們將不勝感激。

回答

0

如果您使用的是bash指定Python路徑中,任何空白應該由\之前。

PYTHONPATH=「/Users/geoffreysametz/google\ drive/programming\ python」 
+0

這確實修復了$ PYTHONPATH,但不是錯誤。 'echo $ PYTHONPATH'返回''/ Users/geoffreysametz/google drive/programming python「',但在」tour「目錄下運行的'python demoAll-prg.py'返回'ImportError:No module named'PP4E'' –

+0

Before demoAll-prg.py中的任何import語句,請嘗試此操作。 import sys;打印(sys.path中);看看是否確實包含了路徑。 – Hun

+0

這揭示了一個奇怪的錯誤。第一個路徑是'/ Users/geoffreysametz/Google Drive/Programming Python/PP4E/GUI/Tour''(大概在路徑B/C程序從這裏啓動),第二個路徑是'/ Users/geoffreysametz/Google驅動/編程Python/PP4E/GUI/Tour /「/用戶/ geoffreysametz/Google Drive /編程Python」「,其中.bash_profile路徑(用雙引號括起來)被複制到前一個路徑的副本中。 –

相關問題