Python 如何在 Windows 上安装 pip?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4750806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:21:12  来源:igfitidea点击:

How to install pip on Windows?

pythonwindowsinstallationpipeasy-install

提问by mit

pipis a replacement for easy_install. But should I install pipusing easy_installon Windows? Is there a better way?

pip是 的替代品easy_install。但是我应该在 Windows 上安装pipusingeasy_install吗?有没有更好的办法?

采纳答案by Colonel Panic

Python 2.7.9+ and 3.4+

Python 2.7.9+ 和 3.4+

Good news! Python 3.4(released March 2014) and Python 2.7.9(released December 2014) ship with Pip. This is the best feature of any Python release. It makes the community's wealth of libraries accessible to everyone. Newbies are no longer excluded from using community libraries by the prohibitive difficulty of setup. In shipping with a package manager, Python joins Ruby, Node.js, Haskell, Perl, Go—almost every other contemporary language with a majority open-source community. Thank you, Python.

好消息!Python 3.4(2014 年 3 月发布)和Python 2.7.9(2014 年 12 月发布)随 Pip 一起提供。这是任何 Python 版本的最佳特性。它使每个人都可以访问社区丰富的图书馆。新手不再因设置的高难度而被排除在使用社区库之外。在用的软件包管理器出货,Python中加入的RubyNode.js的哈斯克尔Perl中-almost与大多数开源社区所有其他现代语言。谢谢你,蟒蛇。

If you do find that pip is not available when using Python 3.4+ or Python 2.7.9+, simply execute e.g.:

如果您发现在使用 Python 3.4+ 或 Python 2.7.9+ 时 pip 不可用,只需执行例如:

py -3 -m ensurepip

Of course, that doesn't mean Python packaging is problem solved. The experience remains frustrating. I discuss this in the Stack Overflow question Does Python have a package/module management system?.

当然,这并不意味着 Python 打包问题就解决了。这种经历仍然令人沮丧。我在 Stack Overflow 问题中讨论了这个问题Python 是否有包/模块管理系统?.

And, alas for everyone using Python 2.7.8 or earlier (a sizable portion of the community). There's no plan to ship Pip to you. Manual instructions follow.

而且,对于使用 Python 2.7.8 或更早版本(社区的相当大一部分)的每个人来说,唉。没有计划将 Pip 运送给您。手册说明如下。

Python 2 ≤ 2.7.8 and Python 3 ≤ 3.3

Python 2 ≤ 2.7.8 和 Python 3 ≤ 3.3

Flying in the face of its 'batteries included'motto, Python ships without a package manager. To make matters worse, Pip was—until recently—ironically difficult to install.

面对其“包括电池”的座右铭,Python 没有包管理器。更糟糕的是,具有讽刺意味的是,Pip 直到最近都难以安装。

Official instructions

官方说明

Per https://pip.pypa.io/en/stable/installing/#do-i-need-to-install-pip:

根据https://pip.pypa.io/en/stable/installing/#do-i-need-to-install-pip

Download get-pip.py, being careful to save it as a .pyfile rather than .txt. Then, run it from the command prompt:

下载get-pip.py,小心将其保存为.py文件而不是.txt. 然后,从命令提示符运行它:

python get-pip.py

You possibly need an administrator command prompt to do this. Follow Start a Command Prompt as an Administrator(Microsoft TechNet).

您可能需要管理员命令提示符来执行此操作。按照以管理员身份启动命令提示符(Microsoft TechNet)。

This installs the pip package, which (in Windows) contains ...\Scripts\pip.exe that path must be in PATH environment variable to use pip from the command line (see the second part of 'Alternative Instructions' for adding it to your PATH,

这将安装 pip 包,其中(在 Windows 中)包含 ...\Scripts\pip.exe 路径必须在 PATH 环境变量中才能从命令行使用 pip(请参阅“替代说明”的第二部分以将其添加到你的路径,

Alternative instructions

替代说明

The official documentation tells users to install Pip and each of its dependencies from source. That's tedious for the experienced and prohibitively difficult for newbies.

官方文档告诉用户从源代码安装 Pip 及其每个依赖项。这对于有经验的人来说是乏味的,对于新手来说是非常困难的。

For our sake, Christoph Gohlke prepares Windows installers (.msi) for popular Python packages. He builds installers for all Python versions, both 32 and 64 bit. You need to:

为了我们,Christoph Gohlke.msi为流行的 Python 包准备了 Windows 安装程序 ( )。他为所有 Python 版本(32 位和 64 位)构建安装程序。你需要:

  1. Install setuptools
  2. Install pip
  1. 安装安装工具
  2. 安装点

For me, this installed Pip at C:\Python27\Scripts\pip.exe. Find pip.exeon your computer, then add its folder (for example, C:\Python27\Scripts) to your path (Start / Edit environment variables). Now you should be able to run pipfrom the command line. Try installing a package:

对我来说,这在C:\Python27\Scripts\pip.exe. pip.exe在您的计算机上查找,然后将其文件夹(例如C:\Python27\Scripts)添加到您的路径(启动/编辑环境变量)。现在您应该可以从命令行运行pip了。尝试安装一个包:

pip install httpie

There you go (hopefully)! Solutions for common problems are given below:

你去(希望)!常见问题的解决方法如下:

Proxy problems

代理问题

If you work in an office, you might be behind an HTTP proxy. If so, set the environment variables http_proxyand https_proxy. Most Python applications (and other free software) respect these. Example syntax:

如果您在办公室工作,您可能会使用 HTTP 代理。如果是这样,请设置环境变量http_proxyhttps_proxy. 大多数 Python 应用程序(和其他免费软件)都尊重这些。示例语法:

http://proxy_url:port
http://username:password@proxy_url:port

If you're really unlucky, your proxy might be a Microsoft NTLMproxy. Free software can't cope. The only solution is to install a free software friendly proxy that forwards to the nasty proxy. http://cntlm.sourceforge.net/

如果您真的不走运,您的代理可能是 Microsoft NTLM代理。免费软件无法应对。唯一的解决方案是安装一个免费的软件友好代理,转发到讨厌的代理。http://cntlm.sourceforge.net/

Unable to find vcvarsall.bat

无法找到 vcvarsall.bat

Python modules can be partly written in C or C++. Pip tries to compile from source. If you don't have a C/C++ compiler installed and configured, you'll see this cryptic error message.

Python 模块可以部分用 C 或 C++ 编写。Pip 尝试从源代码编译。如果您没有安装和配置 C/C++ 编译器,您将看到这个神秘的错误消息。

Error: Unable to find vcvarsall.bat

错误:无法找到 vcvarsall.bat

You can fix that by installing a C++ compilersuch as MinGWor Visual C++. Microsoft actually ships one specifically for use with Python. Or try Microsoft Visual C++ Compiler for Python 2.7.

您可以通过安装 C++ 编译器(例如MinGWVisual C++ )来解决此问题。微软实际上提供了一个专门用于 Python 的。或者尝试Microsoft Visual C++ Compiler for Python 2.7

Often though it's easier to check Christoph's sitefor your package.

通常,尽管检查Christoph 的站点以获取您的包裹更容易。

回答by Rafe Kettler

When I have to use Windows, I use ActivePython, which automatically adds everything to your PATH and includes a package manager called PyPMwhich provides binarypackage management making it faster and simpler to install packages.

当我必须使用 Windows 时,我使用 ActivePython,它会自动将所有内容添加到您的 PATH 中,并包含一个名为PyPM的包管理器,它提供二进制包管理,使安装包变得更快、更简单。

pipand easy_installaren't exactly the same thing, so there are some things you can get through pipbut not easy_installand vice versa.

pip并且easy_install不完全相同,所以有些事情你可以通过pip但不能easy_install,反之亦然

My recommendation is that you get ActivePython Community Editionand don't worry about the huge hassle of getting everything set up for Python on Windows. Then, you can just use pypm.

我的建议是您获得ActivePython 社区版,而不必担心在 Windows 上为 Python 设置所有内容的巨大麻烦。然后,您可以使用pypm.

In case you want to use pipyou have to check the PyPMoption in the ActiveState installer. After installation you only need to logoff and log on again, and pipwill be available on the commandline, because it is contained in the ActiveState installer PyPMoption and the paths have been set by the installer for you already. PyPMwill also be available, but you do not have to use it.

如果您想使用,pip您必须检查PyPMActiveState 安装程序中的选项。安装后,您只需注销并再次登录,pip即可在命令行中使用,因为它包含在 ActiveState 安装程序PyPM选项中,并且安装程序已经为您设置了路径。PyPM也将可用,但您不必使用它。

回答by Adrián Deccico

-- Outdated-- use distribute, not setuptools as described here. --
-- Outdated #2-- use setuptools as distribute is deprecated.

--过时-- 使用分发工具,而不是此处描述的设置工具。--
--过时的#2-- 不推荐使用安装工具,因为不推荐使用分发工具。

As you mentioned pip doesn't include an independent installer, but you can install it with its predecessor easy_install.

正如您提到的 pip 不包含独立安装程序,但您可以使用其前身 easy_install 安装它。

So:

所以:

  1. Download the last pip version from here: http://pypi.python.org/pypi/pip#downloads
  2. Uncompress it
  3. Download the last easy installer for Windows: (download the .exe at the bottom of http://pypi.python.org/pypi/setuptools). Install it.
  4. copy the uncompressed pip folder contentinto C:\Python2x\folder (don't copy the whole folder into it, just the content), because python command doesn't work outside C:\Python2xfolder and then run: python setup.py install
  5. Add your python C:\Python2x\Scriptsto the path
  1. 从这里下载最后一个 pip 版本:http: //pypi.python.org/pypi/pip#downloads
  2. 解压
  3. 下载适用于 Windows 的最后一个简单安装程序:(下载位于http://pypi.python.org/pypi/setuptools底部的 .exe)。安装它。
  4. 将未压缩的 pip 文件夹内容复制到C:\Python2x\文件夹中(不要将整个文件夹复制到其中,只复制内容),因为 python 命令在C:\Python2x文件夹外不起作用,然后运行: python setup.py install
  5. 将你的python添加C:\Python2x\Scripts到路径中

You are done.

你完成了。

Now you can use pip install packageto easily install packages as in Linux :)

现在您可以pip install package像在 Linux 中一样轻松地安装软件包:)

回答by Dylan Hogg

To install pip globallyon Python 2.x, easy_install appears to be the best solution as Adrián states.

要在 Python 2.x 上全局安装 pip ,easy_install 似乎是 Adrián 所说的最佳解决方案。

However the installation instructionsfor pip recommend using virtualenvsince every virtualenv has pip installed in it automatically. This does not require root access or modify your system Python installation.

但是pip的安装说明建议使用virtualenv,因为每个 virtualenv 都自动安装了 pip。这不需要 root 访问权限或修改您的系统 Python 安装。

Installing virtualenv still requires easy_install though.

不过,安装 virtualenv 仍然需要 easy_install。

2018 update:

2018年更新:

Python 3.3+ now includes the venv modulefor easily creating virtual environments like so:

Python 3.3+ 现在包含venv 模块,用于轻松创建虚拟环境,如下所示:

python3 -m venv /path/to/new/virtual/environment

python3 -m venv /path/to/new/virtual/environment

See documentation for different platform methods of activating the environment after creation, but typically one of:

有关在创建后激活环境的不同平台方法,请参阅文档,但通常是以下之一:

$ source <venv>/bin/activate 

C:\> <venv>\Scripts\activate.bat

回答by AndrewPK

2014 UPDATE:

2014年更新:

1) If you have installed Python 3.4 or later, pip is included with Python and should already be working on your system.

1) 如果您安装了 Python 3.4 或更高版本,则 pip 包含在 Python 中并且应该已经在您的系统上运行。

2) If you are running a version below Python 3.4 or if pip was not installed with Python 3.4 for some reason, then you'd probably use pip's official installation script get-pip.py. The pip installer now grabs setuptools for you, and works regardless of architecture (32-bit or 64-bit).

2) 如果您运行的是低于 Python 3.4 的版本,或者由于某种原因 pip 没有与 Python 3.4 一起安装,那么您可能会使用 pip 的官方安装脚本get-pip.py。pip 安装程序现在为您获取 setuptools,并且无论架构如何(32 位或 64 位)都可以工作。

The installation instructions are detailed hereand involve:

安装说明在此处详细说明,包括:

To install or upgrade pip, securely download get-pip.py.

Then run the following (which may require administrator access):

要安装或升级 pip,请安全下载get-pip.py

然后运行以下命令(可能需要管理员访问权限):

python get-pip.py

To upgrade an existing setuptools (or distribute), run pip install -U setuptools

要升级现有的安装工具(或分发),请运行 pip install -U setuptools

I'll leave the two sets of old instructions below for posterity.

我将把下面的两套旧说明留给后人。

OLD Answers:

旧答案:

For Windows editions of the 64 bitvariety - 64-bit Windows + Python used to require a separate installation method due to ez_setup, but I've tested the new distribute method on 64-bit Windows running 32-bit Python and 64-bit Python, and you can now use the same method for all versions of Windows/Python 2.7X:

对于64 位版本的 Windows 版本- 由于 ez_setup,64 位 Windows + Python 过去需要单独的安装方法,但我已经在运行 32 位 Python 和 64 位 Python 的 64 位 Windows 上测试了新的分发方法,您现在可以对所有版本的 Windows/Python 2.7X 使用相同的方法:

OLD Method 2using distribute:

旧方法 2使用分发

  1. Download distribute- I threw mine in C:\Python27\Scripts(feel free to create a Scriptsdirectory if it doesn't exist.
  2. Open up a command prompt (on Windows you should check out conemu2if you don't use PowerShell) and change (cd) to the directory you've downloaded distribute_setup.pyto.
  3. Run distribute_setup: python distribute_setup.py(This will not work if your python installation directory is not added to your path - go here for help)
  4. Change the current directory to the Scriptsdirectory for your Python installation (C:\Python27\Scripts) or add that directory, as well as the Python base installation directory to your %PATH% environment variable.
  5. Install pip using the newly installed setuptools: easy_install pip
  1. 下载分发- 我扔了我的C:\Python27\ScriptsScripts如果目录不存在,请随意创建一个目录。
  2. 打开命令提示符(在 Windows 上,如果您不使用PowerShell,则应检查conmu2)并将 ( )更改为您下载到的目录。cddistribute_setup.py
  3. 运行distribute_setup:(python distribute_setup.py如果你的python安装目录没有添加到你的路径中,这将不起作用 -到这里寻求帮助
  4. 将当前目录更改Scripts为 Python 安装目录 ( C:\Python27\Scripts) 或将该目录以及 Python 基本安装目录添加到 %PATH% 环境变量。
  5. 使用新安装的 setuptools 安装 pip: easy_install pip

The last step will not work unless you're either in the directory easy_install.exeis located in (C:\Python27\Scripts would be the default for Python 2.7), or you have that directory added to your path.

最后一步将不起作用,除非您easy_install.exe位于目录中(C:\Python27\Scripts 将是 Python 2.7 的默认值),或者您将该目录添加到您的路径中。

OLD Method 1using ez_setup:

使用 ez_setup 的旧方法 1

from the setuptools page--

从 setuptools 页面-

Download ez_setup.pyand run it; it will download the appropriate .egg file and install it for you. (Currently, the provided .exe installer does not support 64-bit versions of Python for Windows, due to a distutils installer compatibility issue.

下载ez_setup.py并运行;它将下载适当的 .egg 文件并为您安装。(目前,由于 distutils 安装程序兼容性问题,提供的 .exe 安装程序不支持 64 位版本的 Python for Windows。

After this, you may continue with:

在此之后,您可以继续:

  1. Add c:\Python2x\Scriptsto the Windows path (replace the xin Python2xwith the actual version number you have installed)
  2. Open a new (!) DOS prompt. From there run easy_install pip
  1. 添加c:\Python2x\Scripts到 Windows 路径(将xin替换为Python2x您已安装的实际版本号)
  2. 打开一个新的 (!) DOS 提示符。从那里跑easy_install pip

回答by user1233104

I just wanted to add one more solution for those having issues installing setuptools from Windows 64-bit. The issue is discussed in this bug on python.org and is still unresolved as of the date of this comment. A simple workaround is mentioned and it works flawlessly. One registry change did the trick for me.

我只是想为那些在从 Windows 64 位安装 setuptools 时遇到问题的人添加另一个解决方案。该问题在 python.org 上的此错误中进行了讨论,截至本评论发布之日仍未解决。提到了一个简单的解决方法,它可以完美运行。一项注册表更改对我有用。

Link: http://bugs.python.org/issue6792#

链接:http: //bugs.python.org/issue6792#

Solution that worked for me...:

对我有用的解决方案...:

Add this registry setting for 2.6+ versions of Python:

为 2.6+ 版本的 Python 添加此注册表设置:

 [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore.6\InstallPath]
 @="C:\Python26\"

This is most likely the registry setting you will already have for Python 2.6+:

这很可能是您已经拥有的 Python 2.6+ 的注册表设置:

 [HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore.6\InstallPath]
 @="C:\Python26\"

Clearly, you will need to replace the 2.6 version with whatever version of Python you are running.

显然,您需要用您正在运行的任何版本的 Python 替换 2.6 版本。

回答by David Fraser

Installers

安装人员

I've built Windows installers for both distributeand piphere (the goal being to use pipwithout having to either bootstrap with easy_installor save and run Python scripts):

我在这里为分发pip构建了 Windows 安装程序(目标是使用pip而无需引导easy_install或保存和运行 Python 脚本):

On Windows, simply download and install first distribute, then pipfrom the above links. The distributelink above does contain stub .exeinstallers, and these are currently 32-bit only. I haven't tested the effect on 64-bit Windows.

在 Windows 上,只需先下载并安装distribute,然后pip从上面的链接下载。distribute上面的链接确实包含存根.exe安装程序,这些目前只有 32 位。我还没有在 64 位 Windows 上测试过效果。

Building on Windows

在 Windows 上构建

The process to redo this for new versions is not difficult, and I've included it here for reference.

为新版本重做此过程并不困难,我已将其包含在此处以供参考。

Building distribute

建筑 distribute

In order to get the stub .exefiles, you need to have a Visual C++ compiler (it is apparently compilable with MinGW as well)

为了获得存根.exe文件,你需要有一个 Visual C++ 编译器(它显然也可以用 MinGW 编译)

hg clone https://bitbucket.org/tarek/distribute
cd distribute
hg checkout 0.6.27
rem optionally, comment out tag_build and tag_svn_revision in setup.cfg
msvc-build-launcher.cmd
python setup.py bdist_win32
cd ..
echo build is in distribute\dist

Building pip

建筑 pip

git clone https://github.com/pypa/pip.git
cd pip
git checkout 1.1
python setup.py bdist_win32
cd ..
echo build is in pip\dist

回答by RayLuo

Updated at 2016 :Pipshould already be included in Python 2.7.9+ or 3.4+, but if for whatever reason it is not there, you can use the following one-liner.

2016 年更新:Pip应该已经包含在 中Python 2.7.9+ or 3.4+,但如果由于某种原因它不存在,您可以使用以下单行。

PS:

PS:

  1. This should already be satisfied in most cases but, if necessary, be sure that your environment variable PATH includes Python's folders (for example, Python 2.7.xon Windows default install: C:\Python27 and C:\Python27\Scripts, for Python 3.3x: C:\Python33 and C:\Python33\Scripts, etc)

  2. I encounter same problem and then found such perhaps easiest way (one liner!) mentioned on official website here: http://www.pip-installer.org/en/latest/installing.html

  1. 这在大多数情况下应该已经得到满足,但如果需要,请确保您的环境变量 PATH 包含 Python 的文件夹(例如,Python 2.7.x在 Windows 默认安装:C:\Python27 and C:\Python27\Scripts,for Python 3.3xC:\Python33 and C:\Python33\Scripts等)

  2. 我遇到了同样的问题,然后找到了官方网站上提到的这种可能最简单的方法(一个班轮!):http: //www.pip-installer.org/en/latest/installing.html

Can't believe there are so many lengthy (perhaps outdated?) answers out there. Feeling thankful to them but, please up-vote this short answer to help more new comers!

不敢相信有这么多冗长的(也许已经过时?)答案。对他们心存感激,但是,请为这个简短的答案投票,以帮助更多的新人!

回答by Gringo Suave

2016+ Update:

2016+ 更新:

These answers are outdated or otherwise wordy and difficult.

这些答案已经过时或冗长而困难。

If you've got Python 3.4+ or 2.7.9+, it will be installed by defaulton Windows. Otherwise, in short:

如果您有 Python 3.4+ 或 2.7.9+,它将默认安装在 Windows 上。否则,简而言之:

  1. Download the pip installer: https://bootstrap.pypa.io/get-pip.py
  2. If paranoid, inspect file to confirm it isn't malicious (must b64 decode).
  3. Open a console in the download folder as Admin and run get-pip.py. Alternatively, right-click its icon in Explorer and choose the "run as Admin...".
  1. 下载 pip 安装程序:https: //bootstrap.pypa.io/get-pip.py
  2. 如果偏执,检查文件以确认它不是恶意的(必须 b64 解码)。
  3. 以管理员身份打开下载文件夹中的控制台并运行 get-pip.py. 或者,在资源管理器中右键单击其图标并选择“以管理员身份运行...”。

The new binaries pip.exe(and the deprecated easy_install.exe) will be found in the "%ProgramFiles%\PythonXX\Scripts"folder (or similar), which is often not in your PATHvariable. I recommend adding it.

新的二进制文件pip.exe(和已弃用的easy_install.exe)将在"%ProgramFiles%\PythonXX\Scripts"文件夹(或类似文件)中找到,该文件夹通常不在您的PATH变量中。我建议添加它。

回答by Fernando Macedo

Update March 2015

2015 年 3 月更新

Python 2.7.9 and later (on the Python 2 series), and Python 3.4 and later include pip by default, so you may have pip already.

Python 2.7.9 及更高版本(在 Python 2 系列上)和 Python 3.4 及更高版本默认包含 pip,因此您可能已经拥有 pip。

If you don't, run this one line command on your prompt (which may require administrator access):

如果不这样做,请在提示符下运行此单行命令(可能需要管理员访问权限):

python -c "exec('try: from urllib2 import urlopen \nexcept: from urllib.request import urlopen');f=urlopen('https://bootstrap.pypa.io/get-pip.py').read();exec(f)"

It will install pip. If Setuptoolsis not already installed, get-pip.pywill install it for you too.

它将安装pip。如果尚未安装Setuptoolsget-pip.py也会为您安装它。

As mentioned in comments, the above command will download code from the Pip source code repository at GitHub, and dynamically run it at your environment. So be noticed that this is a shortcut of the steps download, inspect and run, all with a single command using Python itself. If you trust Pip, proceed without doubt.

如评论中所述,上述命令将从GitHub的 Pip 源代码存储库下载代码,并在您的环境中动态运行它。所以请注意,这是下载、检查和运行步骤的快捷方式,所有这些都是使用 Python 本身的单个命令。如果您信任 Pip,请毫无疑问地继续。

Be sure that your Windows environment variable PATH includes Python's folders (for Python 2.7.x default install: C:\Python27and C:\Python27\Scripts, for Python 3.3x: C:\Python33and C:\Python33\Scripts, and so on).

确保您的 Windows 环境变量 PATH 包括 Python 的文件夹(对于 Python 2.7.x 默认安装:C:\Python27C:\Python27\Scripts,对于 Python 3.3x:C:\Python33C:\Python33\Scripts,等等)。