【问题标题】:ipython can't load a module which python doesipython 无法加载 python 执行的模块
【发布时间】:2015-10-05 12:49:33
【问题描述】:

我最近更新了 OSX El Capitain,升级了现有的工作 osx 安装。现在我有一个模块,通过 pip 安装在现在已损坏的 anaconda 发行版上。或者更好的模块可以从python正确导入

Python 2.7.10 |Anaconda 2.3.0 (x86_64)| (default, Sep 15 2015, 14:29:08) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import MDSplus as mds

虽然它没有在 ipython 上加载,但声称没有找到库。

Python 2.7.10 |Anaconda 2.3.0 (x86_64)| (default, Sep 15 2015, 14:29:08) 
Type "copyright", "credits" or "license" for more information.

IPython 4.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
Using matplotlib backend: MacOSX

In [1]: import MDSplus as mds
Error importing MDSplus package: Error finding library: MdsShr

该库已正确安装,它在/etc/profile 中列出,它位于/usr/local(因此OSX el Captain 的新安全系统应该没有问题)并且python 解释为通过 python 或 ipython 调用是相同的(Anaconda 2.3.0)。 如何诊断问题所在?

编辑: https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/PEuOKEM5fdc 中提出的解决方案对我不起作用。好像不是环境问题和命令

echo $DYLD_LIBRARY_PATH

指向正确的路径

编辑:

which -a ipython
ipython is /Users/vianello/anaconda/bin/ipython
which -a python 
python is /Users/vianello/anaconda/bin/python
python is /usr/bin/python

>>> import sys
>>> for x in sys.path: print x
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/pyhht-0.0.1-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/MitDevices-0.3-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/MDSplus-alpha_7.0.157-py2.7.egg
>>>//anaconda/lib/python2.7/site-packages/MDSplus-alpha_7.0.147-py2.7.egg/MDSplus
>>>/Users/vianello/anaconda/lib/python27.zip
>>>/Users/vianello/anaconda/lib/python2.7
>>>/Users/vianello/anaconda/lib/python2.7/plat-darwin
>>>/Users/vianello/anaconda/lib/python2.7/plat-mac
>>>/Users/vianello/anaconda/lib/python2.7/plat-mac/lib-scriptpackages
>>>/Users/vianello/anaconda/lib/python2.7/lib-tk
>>>/Users/vianello/anaconda/lib/python2.7/lib-old
>>>/Users/vianello/anaconda/lib/python2.7/lib-dynload
>>>/Users/vianello/anaconda/lib/python2.7/site-packages
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/PIL
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/Sphinx-1.3.1-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/aeosa
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/lmfit-0.8.3-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/setuptools-18.3.2-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/suds-0.4-py2.7.egg

在 iPython 中同样给出结果

/Users/vianello/anaconda/bin
/Users/vianello/anaconda/lib/python2.7/site-packages/pyhht-0.0.1-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/MitDevices-0.3-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/MDSplus-alpha_7.0.157-py2.7.egg
/Users/vianello/anaconda/lib/python27.zip
/Users/vianello/anaconda/lib/python2.7
/Users/vianello/anaconda/lib/python2.7/plat-darwin
/Users/vianello/anaconda/lib/python2.7/plat-mac
/Users/vianello/anaconda/lib/python2.7/plat-mac/lib-scriptpackages
/Users/vianello/anaconda/lib/python2.7/lib-tk
/Users/vianello/anaconda/lib/python2.7/lib-old
/Users/vianello/anaconda/lib/python2.7/lib-dynload
/Users/vianello/anaconda/lib/python2.7/site-packages/Sphinx-1.3.1-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/lmfit-0.8.3-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/setuptools-18.3.2-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/suds-0.4-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages
/Users/vianello/anaconda/lib/python2.7/site-packages/PIL
/Users/vianello/anaconda/lib/python2.7/site-packages/aeosa
/Users/vianello/anaconda/lib/python2.7/site-packages/IPython/extensions
/Users/vianello/.ipython

因此 MDSplus-alpha_7.0.157-py2.7.egg 可以从 python 和 ipython 中看到

【问题讨论】:

  • 是的,我做到了,提出的解决方案没有解决问题,即添加到 PYTHONPATH 没有解决问题
  • 你说得对,对不起,我已经编辑了问题
  • 你是从 virtualenv 运行 ipython 吗?
  • 不,我没有
  • import syssys.path 来自两个 python,看看模块的路径是否在两者中都可用。

标签: macos python-2.7 ipython


【解决方案1】:

我的第一个想法是您违反了新的 Apple 系统完整性保护。 iPython 使用通过/bin/bash 加载的shell 脚本启动。这将导致您的 DYLD_LIBRARY_PATH 环境变量在 python 启动时被剥离。您可以通过导入os 然后尝试打印os.environ["DYLD_LIBRARY_PATH"] 的内容来检查这一点。如果该打印抛出异常,那么您就知道问题所在了。

一个对我有用的简单修复方法是编辑ipython 脚本的第一行,以便#! 直接调用您的python 二进制文件,而不是通过bash。这不是一个长期的解决方案,因为每次更新 ipython 时都必须重做,直到上游发生变化(公平地说,我不确定为什么涉及 bash)。

我在http://dmtn-001.lsst.io写了一篇关于python和库路径的报告

【讨论】:

    猜你喜欢
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    相关资源
    最近更新 更多