自从Homebrew Python 2.7.11(从2.7.10)更新后,我突然无法在PyCharm IDE控制台上测试在PyPi上注册我的包.
运行(作为"外部工具")
python -B setup.py register -r pypitest
我现在明白了
Traceback (most recent call last): File "setup.py", line 22, infrom setuptools import setup File "/usr/local/lib/python2.7/site-packages/setuptools/__init__.py", line 12, in from setuptools.extension import Extension File "/usr/local/lib/python2.7/site-packages/setuptools/extension.py", line 8, in from .dist import _get_unpatched File "/usr/local/lib/python2.7/site-packages/setuptools/dist.py", line 16, in from setuptools.depends import Require File "/usr/local/lib/python2.7/site-packages/setuptools/depends.py", line 6, in from setuptools import compat File "/usr/local/lib/python2.7/site-packages/setuptools/compat.py", line 17, in import httplib File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 80, in import mimetools File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/mimetools.py", line 6, in import tempfile File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 32, in import io as _io File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/io.py", line 51, in import _io ImportError: dlopen(/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyCodecInfo_GetIncrementalDecoder Referenced from: /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so Expected in: flat namespace in /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so Process finished with exit code 1
我不知道该怎么办.如果我在IDE的控制台中执行,我只会遇到此问题.如果我直接在系统命令行(OS X上的终端)执行此操作,我没有问题.
OS X 10.11.3; 自制Python 2.7.11; PyCharm 5.0.3
tl; dr:通过执行以下操作之一来解决此问题:
类型hash -r python
,或
注销并登录.
编辑:我的相关问题的答案清楚地表明这里发生了什么.当您安装新版本的python时,您可能需要运行hash -r python
以告知bash将"缓存"位置重置为python
可执行文件.
在我的情况下,我正在打字python
,这是在我$PATH
的/usr/local/bin/python
.但bash
仍然使用旧的缓存位置/usr/bin/python
.因此,旧的可执行文件被调用,但新的路径被提供给python in sys.argv[0]
.这意味着旧的可执行文件正在运行,但新sys.executable
值导致所有错误的模块被加载(包括io
模块).
我遇到了同样的问题.我通过Python.org的安装程序安装了python 2.7.11.奇怪的是,这个问题似乎与OSX python
在使用完整路径从shell调用OSX时的启动方式之间的某些细微差别有关python
.
所以,对我来说,这是有效的(通过完整路径调用python /usr/local/bin/python
):
$ which python /usr/local/bin/python $ /usr/local/bin/python -c "import io" $
......但这不是:
$ python -c "import io" Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/io.py", line 51, in import _io ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyCodecInfo_GetIncrementalDecoder Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so Expected in: flat namespace in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
因此,作为一种解决方法,您可以尝试做同样的事情.
在其他地方,我已经发布了一个关于这种令人费解的行为的单独问题.也许不知何故只是调用python
调用2.7.11可执行文件与2.7.10 dylibs的一些奇怪混合?
根据https://github.com/klen/python-mode/issues/634:
我有同样的问题,但成功修复了.在我的情况下,我用自制软件编译python和vim,当指定PYTHON_PATH并设置为我的开发环境之一时,我也有一些库,包括io.解决方法很简单:打开新终端,确保你没有自定义PYTHON_PATH,卸载python,卸载vim.重新安装它们.
和
问题解决了.
Culprit是从python 2.7.10到2.7.11的更新.
如果你使用的是conda包控件,只需运行"conda install python = 2.7.10"就可以解决这个问题.
但这并没有给出根本原因.由于这种情况发生_io
,这看起来像python 2.7.11中的一个错误(不太可能,如果它是世界范围的强烈抗议和提示修复)或某些包装错误或版本与自制软件版本不匹配(可能是一些相关的).
尝试import _io
在控制台中,如果成功,检查它是否从同一路径加载.
重新安装python.
brew unlink python && brew reinstall python
保护路径
export PYTHONPATH=$PYTHONPATH:/usr/local/bin/
备份并更改"路径"文件的顺序.
sudo nano /etc/paths
看起来,路径的顺序,正确运行python是决定性的.就我而言,结果是:
#sudo nano /etc/paths /usr/bin /usr/local/bin /bin /usr/sbin /sbin
在我的Mac上,路径是这样的.
$ which python /usr/local/bin/python
现在我可以同时运行:
$ /usr/local/bin/python -c "import io" $ python -c "import io"
我有同样的问题,只需更换_io.so文件就可以成功修复它.
sudo find / -name _io.so
复制不属于python-2.7.11 的_io.so
文件路径.例如,复制_io.so的路径,该路径位于python-2.7.5下:/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib- dynload/_io.so
用您刚找到的/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
文件替换文件_io.so
.