在virtualenv中安装pip时的ImportError

时间:2014-12-24 09:50:42

标签: python python-2.7 installation pip virtualenv

我有一台安装了python2.6的rhel机器。我一直在尝试替换安装python2.7并设置virtualenv使用2.7。我通过从源代码构建安装了python2.7,如下所示:

./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib" 
make && make altinstall

我已经在系统上安装了virtualenv,所以我用它来创建2.7的venv,如下所示:

virtualenv -p python2.7 --no-setuptools py27
. py27/bin/activate

现在当我尝试在venv中安装pip时,它会失败:

python get-pip.py
Traceback (most recent call last):
File "get-pip.py", line 19857, in <module>
  main()
File "get-pip.py", line 151, in main
  bootstrap(tmpdir=tmpdir)
File "get-pip.py", line 81, in bootstrap
  import pip
File "/tmp/tmpArPs31/pip.zip/pip/__init__.py", line 15, in <module>
File "/tmp/tmpArPs31/pip.zip/pip/vcs/mercurial.py", line 11, in <module>
File "/tmp/tmpArPs31/pip.zip/pip/download.py", line 29, in <module>
File "/tmp/tmpArPs31/pip.zip/pip/_vendor/__init__.py", line 81, in load_module
ImportError: No module named 'pip._vendor.requests'

无法弄清楚这里出了什么问题。请帮忙。

我已经安装了python 2.7.8。

编辑:我最初尝试使用--no-setuptools选项创建virtualenv,但这给了我同样的错误:

virtualenv -p python2.7 py27_with_pip
Running virtualenv with interpreter /usr/local/bin/python2.7
New python executable in py27_with_pip/bin/python2.7
Also creating executable in py27_with_pip/bin/python
Installing setuptools, pip...
  Complete output from command /data1/home/sagraw1/...th_pip/bin/python2.7 -c "import sys, pip;     sys...d\"] + sys.argv[1:]))" setuptools pip:
  Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv_support/pip-6.0-    py2.py3-none-any.whl/pip/__init__.py", line 15, in <module>
  File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv_support/pip-6.0-py2.py3-none-any.whl/pip/vcs/mercurial.py", line 11, in <module>
  File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv_support/pip-6.0-py2.py3-none-any.whl/pip/download.py", line 29, in <module>
  File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv_support/pip-6.0-py2.py3-none-any.whl/pip/_vendor/__init__.py", line 81, in load_module
ImportError: No module named 'pip._vendor.requests'
----------------------------------------
...Installing setuptools, pip...done.
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv.py", line 2363, in <module>
    main()
  File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv.py", line 848, in main
symlink=options.symlink)
  File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv.py", line 1016, in create_environment
    install_wheel(to_install, py_executable, search_dirs)
  File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv.py", line 984, in install_wheel
    'PIP_NO_INDEX': '1'
  File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv.py", line 926, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command /data1/home/sagraw1/...th_pip/bin/python2.7 -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip failed with error code 1

我尝试直接用python2.7安装pip,但即使这样也行不通:

python2.7 get-pip.py
Traceback (most recent call last):
  File "get-pip.py", line 19857, in <module>
    main()
  File "get-pip.py", line 151, in main
    bootstrap(tmpdir=tmpdir)
  File "get-pip.py", line 81, in bootstrap
    import pip
  File "/tmp/tmpPSVEkk/pip.zip/pip/__init__.py", line 15, in <module>
  File "/tmp/tmpPSVEkk/pip.zip/pip/vcs/mercurial.py", line 11, in <module>
  File "/tmp/tmpPSVEkk/pip.zip/pip/download.py", line 29, in <module>
  File "/tmp/tmpPSVEkk/pip.zip/pip/_vendor/__init__.py", line 81, in load_module
ImportError: No module named 'pip._vendor.requests'

1 个答案:

答案 0 :(得分:9)

我遇到了与你类似的情况,我最终找到了解决办法。至少在我的情况下根本问题是当我编译python 2.7.8时,构建过程没有找到合适的OpenSSL库(因为它们没有安装在我的系统上)。在完成运行后,它显示了类似于此的摘要:

Failed to find the necessary bits to build these modules:
_bsddb             _curses            _curses_panel
_hashlib           _sqlite3           _ssl

这并不妨碍make install工作,之后Python工作得很好,除了它无法访问https URL和其他相关功能。您可以通过启动python然后键入

来判断这是否是您的情况
  

导入ssl

如果看到ImportError,则缺少ssl模块。否则,您根本看不到任何消息。

我通过运行(以root身份)安装OpenSSL库来修复此问题:

  

yum install openssl-devel

在此之后,我用make重建了Python(现在_ssl没有列为缺失),然后是make install。在此之后,运行get-pip.py工作顺利。 希望这有帮助!