【问题标题】:Module search path not fixed by adding __init__.py and editing PYTHONPATH通过添加 __init__.py 和编辑 PYTHONPATH 未修复模块搜索路径
【发布时间】:2016-08-09 14:16:10
【问题描述】:

我有一个与this one 非常相似的问题。我也在使用Programming Python 4th edition。我正在使用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

但这并不能解决问题。我已经关闭并重新打开了终端,并重新启动了计算机。

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

【问题讨论】:

    标签: macos python-3.x pythonpath


    【解决方案1】:

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

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

    【讨论】:

    • 这确实修复了 $PYTHONPATH,但不是错误。 echo $PYTHONPATH返回“/Users/geoffreysametz/google drive/programming python”,但是python demoAll-prg.py在“tour”目录下运行返回ImportError: No module named 'PP4E'
    • 在 demoAll-prg.py 中的任何导入语句之前,试试这个。导入系统;打印(系统路径);看看是否确实包含路径。
    • 这揭示了一个奇怪的错误。第一个路径是'/Users/geoffreysametz/Google Drive/Programming Python/PP4E/GUI/Tour'(大概是从这里启动的路径b/c 程序),第二个是'/Users/geoffreysametz/Google Drive/Programming Python/PP4E/GUI/Tour/“/Users/geoffreysametz/Google Drive/Programming Python”',其中.bash_profile 路径(带有双引号)被混搭到前一个路径的副本上。
    • 在您的原始描述中,所有路径名都是小写的。但是在 print 语句中有些是大写的。
    • 对不起,我应该补充一点,我更改了 .bash_profile 中使用的大小写以匹配初始发布后的真实文件名,因为区分大小写可能是个问题。上面的路径是真实的当前输出的直接剪切和粘贴。
    猜你喜欢
    • 2018-09-16
    • 1970-01-01
    • 2016-06-14
    • 2017-05-01
    • 2019-05-29
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    相关资源
    最近更新 更多