python 2.7 => ImportError:没有名为_io的模块


8

我决定从Windows切换到Linux,因此我安装了Ubuntu Raring Final Beta。

不幸的是我有一个Python问题。

我试图pip从我创建的virtualenv中使用,从这样的requirements.txt文件安装软件包:pip install -r requirements.txt

安装软件包时出现以下错误:

buildutils/initlibzmq.c:10:20: fatal error: Python.h: No such file or directory

compilation terminated.

error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

我在互联网上发现要摆脱这种情况,我应该安装python-devdeb软件包:

sudo apt-get install python-dev

由于安装了此软件包,因此在尝试运行python时出现以下错误:

file: "ImportError: No module named _io".

I use this python from the virtualenv: 
Python 2.7.4rc1 (default, Mar 30 2013, 15:39:28) 
[GCC 4.7.2] on linux2

有人有主意吗?我在Google上找不到任何东西,我确实需要解决此问题才能在我的工作中使用Ubuntu。

谢谢你的帮助。

编辑:

看来我的virtualenv由于某种原因已损坏。我卸载了它。

我重新创建了一个,然后再次执行了“ pip install -r requirements.txt”,现在出现此错误:

Warning: failed to configure libzmq:

/bin/sh: 1: ./configure: not found



staging platform.hpp from: buildutils/include_linux



************************************************

Using bundled libzmq

************************************************

building 'zmq.libzmq' extension

creating build/temp.linux-x86_64-2.7/buildutils

creating build/temp.linux-x86_64-2.7/bundled

creating build/temp.linux-x86_64-2.7/bundled/zeromq

creating build/temp.linux-x86_64-2.7/bundled/zeromq/src

x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Ibundled/zeromq/include -Ibundled -I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7 -c buildutils/initlibzmq.c -o build/temp.linux-x86_64-2.7/buildutils/initlibzmq.o

x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Ibundled/zeromq/include -Ibundled -I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7 -c bundled/zeromq/src/ipc_address.cpp -o build/temp.linux-x86_64-2.7/bundled/zeromq/src/ipc_address.o

x86_64-linux-gnu-gcc: error trying to exec 'cc1plus': execvp: No such file or directory

error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

编辑2:

该gcc误差以上可以通过安装固定build-essential包。


当您尝试运行脚本或双关语python句段时,会出现此错误?
— 赛斯2013年

是在运行脚本时。如您所见,我刚刚编辑了问题,现在我的问题略有不同。
— 本杰明·

Answers:


3

除了标准库和第三方模块外,Python还具有内置模块。这些都是用C编写的,并直接在Python可执行文件中链接。您可以找出这些是什么样的:

$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.builtin_module_names
('__builtin__', '__main__', '_ast', '_bisect', '_codecs', '_collections', '_functools', '_heapq', '_io', '_locale', '_md5', '_random', '_sha', '_sha256', '_sha512', '_socket', '_sre', '_struct', '_symtable', '_warnings', '_weakref', 'array', 'binascii', 'cPickle', 'cStringIO', 'cmath', 'errno', 'exceptions', 'fcntl', 'gc', 'grp', 'imp', 'itertools', 'marshal', 'math', 'operator', 'posix', 'pwd', 'select', 'signal', 'spwd', 'strop', 'sys', 'syslog', 'thread', 'time', 'unicodedata', 'xxsubtype', 'zipimport', 'zlib')

virtualenv env通过复制/usr/bin/python2.7到env/bin/python然后将所有标准库模块符号链接到env/lib/python2.7/*.py。

升级系统Python时,会在所有virtualenvs中自动获得更新的标准库(因为符号链接!),但是您仍在使用Python可执行文件的旧版本。这意味着您还使用了内置模块的旧版本。

一些标准库模块依赖于内置模块。有时,Python 2.7。(x + 1)标准库也依赖于Python 2.7。(x + 1)中引入的内置模块。通过使用旧的virtualenv,实际上是在尝试将Python 2.7.x与Python 2.7。(x + 1)的标准库一起使用,有时会中断。

重新创建virtualenv是正确的解决方案。

(假设更新的系统Python中没有新的stdlib文件,那么替换virtualenv中的bin/python和bin/python2.7可执行文件可能就足够了。)


2

您必须在损坏的virtualenv中替换python版本。

做这个:

  1. 创建一个新的Virtualenv

    mkvirtualenv MyNewTestEnv
    
  2. 找到“ python”(在我的情况下,virtualenvs位于主目录中)

    cd ~/.virtualenvs/MyNewTestEnv/bin
    
  3. 将“ python”从新的virtualenv复制到损坏的virtualenv中

    cp ~/.virtualenvs/MyNewTestEnv/bin/python ~/.virtualenvs/<yourCorruptedEnv>/bin
    

而已。现在,您的CorruptedEnv应该已修复。


1

请注意,我添加了此答案以使问题脱离未解决的队列。

当您在第二次编辑中建议自己时,

可以通过安装build-essential debian软件包来修复上述gcc错误。


0

您不需要删除虚拟环境。

只需按照以下步骤。假设您的虚拟环境名称为“ mydev”

1)virtualenv mydev

上面的命令只是升级而不会丢失任何软件包。

2)在virtualenv外部,如果您的pip无法正常工作,则只需“ easy_install pip”即可解决问题

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.