简体   繁体   English

设置虚拟环境:没有名为“pip”的模块

[英]Setting up a virtualenv: No module named 'pip'

I have a fresh install of Python 3.3.4 on a Windows Server 2008 R2 machine.我在 Windows Server 2008 R2 机器上全新安装了 Python 3.3.4。 I've successfully installed the latest versions of Setuptools, Pip and Virtualenv globally:我已经在全球范围内成功安装了最新版本的 Setuptools、Pip 和 Virtualenv:

python ez_setup.py
easy_install pip
pip install virtualenv

Now when I try to set up a virtualenv using virtualenv ENV I get the following stack trace:现在,当我尝试使用virtualenv ENV设置 virtualenv 时,我得到以下堆栈跟踪:

New python executable in ENV\Scripts\python.exe
Installing setuptools, pip...
  Complete output from command [path redacted]\ENV\Scripts\python.exe -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip:
  Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'pip'
----------------------------------------
...Installing setuptools, pip...done.
Traceback (most recent call last):
  File "C:\Python33\lib\runpy.py", line 160, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "C:\Python33\lib\runpy.py", line 73, in _run_code
    exec(code, run_globals)
  File "C:\Python33\Scripts\virtualenv.exe\__main__.py", line 9, in <module>
  File "C:\Python33\lib\site-packages\virtualenv.py", line 824, in main
    symlink=options.symlink)
  File "C:\Python33\lib\site-packages\virtualenv.py", line 992, in create_environment
    install_wheel(to_install, py_executable, search_dirs)
  File "C:\Python33\lib\site-packages\virtualenv.py", line 960, in install_wheel
    'PIP_NO_INDEX': '1'
  File "C:\Python33\lib\site-packages\virtualenv.py", line 902, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command [path redacted]\ENV\Scripts\python.exe -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip failed with error code 1

I've never seen this error before, and the stack trace doesn't make any sense to me.我以前从未见过这个错误,堆栈跟踪对我来说没有任何意义。 I can successfully import pip from a Python shell. Can anyone help me fix this?我可以从 Python shell 成功import pip谁能帮我解决这个问题?

Update The env installs successfully when I supply the --system-site-packages flag.更新当我提供--system-site-packages标志时,env 安装成功。

Useful workaround from the Python bug ticket for anybody else with this issue:来自 Python 错误票证的有用解决方法,适用于其他有此问题的人:

  • Run virtualenv venv --no-setuptools运行virtualenv venv --no-setuptools
  • Activate that virtualenv (venv\\Scripts\\activate)激活那个 virtualenv (venv\\Scripts\\activate)
  • Download and run get-pip.py to manually install pip & setuptools into this virtualenv下载并运行get-pip.py以手动将 pip 和 setuptools 安装到这个 virtualenv 中
  • Continue as normal继续正常

Alternatively, downgrade to 3.3.3.或者,降级到 3.3.3。 This should be fixed properly in 3.3.5这应该在 3.3.5 中正确修复

In my case the simply running the below command resolved the issue;就我而言,只需运行以下命令即可解决问题; however, this command cause the pip to roll back to the previous version.但是,此命令会导致 pip 回滚到以前的版本。

python -m ensurepip --default-pip

Annoyingly, it looks like this might be a Python bug.令人讨厌的是,这看起来可能是一个 Python 错误。 https://github.com/pypa/virtualenv/issues/564 references http://bugs.python.org/issue20621 , which is still open but looks like it's going to be fixed in 3.3.5. https://github.com/pypa/virtualenv/issues/564引用了http://bugs.python.org/issue20621 ,它仍然是开放的,但看起来它将在 3.3.5 中修复。

Dropping my Python installation back down to 3.3.3 fixed the issue.将我的 Python 安装回落到 3.3.3 解决了这个问题。

I know it says Update in bold at the bottom of your question, but my colleague and I both missed that you answered your own question.我知道它在您的问题底部以粗体显示更新,但我和我的同事都错过了您回答自己的问题。

So, in case anyone else might miss it, try the --system-site-packages flag.因此,如果其他人可能会错过它,请尝试使用--system-site-packages标志。 This worked for us:这对我们有用:

virtualenv ENV --system-site-packages

I deactivated the virtual environment using deactivate command.我使用deactivate命令停用虚拟环境。 And then I manually deleted the virtual environment folder and recreated it using the command python -m venv ./venv .然后我手动删除了虚拟环境文件夹并使用命令python -m venv ./venv重新创建它。 This solved my problem.这解决了我的问题。 However, this will obviously delete all the packages and modules you installed as part of your virtual environment - but if pip is missing you probably haven't installed many.但是,这显然会删除您作为虚拟环境的一部分安装的所有软件包和模块 - 但如果缺少 pip,您可能没有安装很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM