通过添加__init__.py并编辑PYTHONPATH来修复模块搜索路径

时间:2016-04-18 00:51:27

标签: macos python-3.x pythonpath

我遇到的问题与this one非常相似。我也在使用 Programming Python第4版。我正在使用mac powerbook。我的目录结构类似于:

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。这也符合 Learning Python,5th ed。,p。 709。

我尝试了几种不同的编辑python路径的方法,例如描述here,但无济于事。目前我的.bash_profile已添加到最后:

PYTHONPATH=“/Users/geoffreysametz/google drive/programming python”
export PYTHONPATH

但这不是解决问题的方法。我关闭并重新打开了终端,并重新启动了计算机。

非常感谢有关如何解决此问题的任何建议。

1 个答案:

答案 0 :(得分:0)

如果使用bash指定python路径,则任何空格都应以\开头。

PYTHONPATH=“/Users/geoffreysametz/google\ drive/programming\ python”