在virtualenv中安装pip时出现ImportError

在virtualenv中安装pip时出现ImportError

本文介绍了在virtualenv中安装pip时出现ImportError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a rhel machine with python2.6 installed on it. I've been trying to have an alternate install of python2.7 and set up a virtualenv for using 2.7. I installed python2.7 by building from source as follows:

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

我已经在系统上安装了virtualenv,因此我使用它为2.7创建了venv,如下所示:

I already had virtualenv installed on the system so I used it to create a venv for 2.7 as follows:

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

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

Now when I try to install pip inside the venv, it fails like so:

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'

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

Can't figure out what's going wrong here. Please help.

我已经安装了python 2.7.8.

I've installed python 2.7.8.

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

I initially tried creating virtualenv without --no-setuptools option but that gave me the same error:

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,但即使这样也不起作用:

I tried installing pip with python2.7 directly but even that doesn't work:

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'

推荐答案

我处于与您类似的情况,最终找到了解决办法.至少在我看来,根本问题是当我编译python 2.7.8时,构建过程没有找到正确的OpenSSL库(因为它们未安装在我的系统上). make运行完毕后,显示类似于以下内容的摘要:

I was in a situation similar to yours, and I eventually found the fix. At least in my case the root problem was that when I compiled python 2.7.8 the build process didn't find the proper OpenSSL libraries (because they were not installed on my system). After make finished running it showed a summary similar to this:

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

这不会阻止make install的运行,在那之后Python可以正常工作,只是它无法访问https URL和其他相关功能.您可以通过启动python然后输入

This doesn't prevent make install from working, and after that Python works just fine, except that it can't access https URLs and other related functionality. You can tell if this is your case by starting python and then typing

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

If you see an ImportError, then the ssl module is missing. Otherwise, you see no message at all.

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

I fixed this by installing the OpenSSL libraries by running (as root):

安装到位后,我用make重建了Python(现在_ssl不再没有丢失),然后进行make install.此后,运行get-pip.py即可顺利进行.希望这会有所帮助!

After this was in place, I rebuilt Python with make (now _ssl was not listed as missing), followed by make install. After this, running get-pip.py worked without a hitch.Hope this helps!

这篇关于在virtualenv中安装pip时出现ImportError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:26