【问题标题】:How do I install Python packages on Windows?如何在 Windows 上安装 Python 包?
【发布时间】:2010-11-29 18:53:47
【问题描述】:

我很难设置 python 包。 SetupTools 的 EasyInstall 应该可以帮助解决这个问题,但他们没有 Python 2.6 的可执行文件。

例如安装 Mechanize,我只是应该根据 INSTALL.txt 将 Mechanize 文件夹放在 C:\Python24\Lib\site-packages 中,但是运行测试不起作用。有人可以帮助阐明这一点吗?谢谢!

【问题讨论】:

标签: python pip


【解决方案1】:

通过命令提示符升级 pip(Python 目录)

D:\Python 3.7.2>python -m pip install --upgrade pip

现在您可以安装所需的模块了

D:\Python 3.7.2>python -m pip install <<yourModuleName>>

【讨论】:

    【解决方案2】:

    pip是python的包安装器,先更新,再下载需要的东西

    python -m pip install --upgrade pip
    

    然后:

    python -m pip install <package_name>
    

    【讨论】:

      【解决方案3】:

      正如 Blauhirn 在预装 2.7 pip 后所说的那样。如果它不适合您,则可能需要将其添加到路径中。

      但是,如果您运行 Windows 10,则不再需要打开终端来安装模块。打开 Python 也是如此。

      您可以直接在搜索菜单中输入pip install mechanize,选择命令即可安装:

      如果出现任何问题,它可能会在您阅读错误之前关闭,但它仍然是一个有用的快捷方式。

      【讨论】:

      • 在使用 VSCode 正常安装 Python 3.8.3 后,我的开始菜单中没有此快捷方式,也许较新的版本再次删除了此快捷方式?
      • 不应该的。 Python是否添加到路径中? @洛伦兹
      • 我已经安装了普通的 Python 3.8.3,并且我在用户定义的环境变量的路径变量中添加了:C:\Users\USER\AppData\Local\Programs\Python\Python38-32 C:\Users\USER\AppData\Local\Programs\Python\Python38-32\Scripts @Simon
      【解决方案4】:

      Windows 的较新版本的 Python 带有 pip 包管理器。 (source)

      如果您使用的是 Python 2 >=2.7.9 或 Python 3 >=3.4,则已安装 pip

      使用它来安装包:

      cd C:\Python\Scripts\
      pip.exe install <package-name>
      

      所以你的情况是:

      pip.exe install mechanize
      

      【讨论】:

      • +1 因为这明确说明在哪里键入“pip install ” 大多数其他指南,在这里和其他地方,几乎理所当然地认为我会知道这一点。
      • 正如所说,这是一个很好的答案!它当然假设 python 安装在 c 驱动器中。对于使用 PowerShell 的人,如果您将 python 安装到路径中有空格的目录,请使用 cd ("path") 更改目录。
      • 这在 Windows 10 上运行良好,即使在 Python 2.7 上也是如此。感谢面向 Windows 的回答,没有让我眼前一亮。
      • 对于某些 Windows 安装,pip 位于 C:\Users\[you]\AppData\Local\Programs\Python\Python[XX]\Scripts\pip
      • python -m pip install &lt;module_name&gt;?
      【解决方案5】:
      PS D:\simcut>  C:\Python27\Scripts\pip.exe install networkx
      Collecting networkx
      c:\python27\lib\site-packages\pip\_vendor\requests\packages\urllib3\util\ssl_.py:318: SNIMissingWarning: An HTTPS reques
      t has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may caus
      e the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer ve
      rsion of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissi
      ngwarning.
        SNIMissingWarning
      c:\python27\lib\site-packages\pip\_vendor\requests\packages\urllib3\util\ssl_.py:122: InsecurePlatformWarning: A true SS
      LContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL con
      nections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.
      readthedocs.io/en/latest/security.html#insecureplatformwarning.
        InsecurePlatformWarning
        Downloading networkx-1.11-py2.py3-none-any.whl (1.3MB)
          100% |################################| 1.3MB 664kB/s
      Collecting decorator>=3.4.0 (from networkx)
        Downloading decorator-4.0.11-py2.py3-none-any.whl
      Installing collected packages: decorator, networkx
      Successfully installed decorator-4.0.11 networkx-1.11
      c:\python27\lib\site-packages\pip\_vendor\requests\packages\urllib3\util\ssl_.py:122: InsecurePlatformWarning: A true SSLContext object i
      s not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade
      to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplat
      formwarning.
        InsecurePlatformWarning
      

      或者只是将目录放到系统路径中的 pip 可执行文件中。

      【讨论】:

        【解决方案6】:

        从 Python 2.7 开始,默认包含 pip。只需通过

        下载您想要的包
        python -m pip install [package-name]
        

        【讨论】:

        • -m 指的是什么?
        • @delinco m **module-name**: Searches **sys.path** for the named module and runs the corresponding **.py** file as a script..
        • 添加环境路径到系统或使用python -m pip install [package-name]之前定位并进入python文件夹
        【解决方案7】:

        accepted answer 已过时。所以首先,pip 优于 easy_install,(Why use pip over easy_install?)。然后按照这些步骤在Windows上安装pip,很简单。

        1. 安装setuptools:

          curl https://bootstrap.pypa.io/ez_setup.py | python
          
        2. 安装pip:

          curl https://bootstrap.pypa.io/get-pip.py | python
          
        3. (可选)您可以将路径添加到您的环境,以便可以在任何地方使用pip。类似于C:\Python33\Scripts

        【讨论】:

        • 在所有建议中,这是唯一对我有用的。我的环境 - Python 3.3.1,Windows 7 64bit,x64 PC。如果可能,请点赞,为大家省去很多麻烦。
        • 以上对我有用,但第二次使用 https 的 curl 发出了关于 ssl 证书已过期的警告。必须在 curl 命令行上使用 --insecure
        • 第一个答案仍然有效,尽管 PIP 优于 IMO
        • 以上对我有用(Windows7 x64,python 2.7)但要安装 pip 我必须按照说明@pip.pypa.io/en/latest/installing.html
        • 安装python 2.7.10,默认pip在里面。然后在cmd中使用python -m pip install Django
        【解决方案8】:

        您不需要 setuptools 的可执行文件。 可以下载源码,解压,遍历下载的目录,在命令提示符下运行python setup.py install

        【讨论】:

        • 感谢您的回复!我在哪里解压它以及在哪里运行该命令?
        • 如果您下载了 tar 文件,只需使用 WinRAR 等程序解压即可。完成此操作后,打开命令提示符并导航到解压缩的文件夹(如果您看到 setup.py 文件,您将知道您在正确的位置)。从那里你可以运行python setup.py install,它会为你安装它。
        【解决方案9】:

        正如我wrote elsewhere

        用 Python 打包是可怕的。根本原因是该语言没有包管理器。

        幸运的是,有一个 Python 包管理器,名为 Pip。 Pip 的灵感来自 Ruby 的 Gem,但缺少一些功能。具有讽刺意味的是,Pip 本身就是complicated to install。在流行的 64 位 Windows 上安装需要从源代码构建和安装两个包。对于刚接触编程的人来说,这是一个很大的要求。


        所以正确的做法是安装 pip。但是,如果您不介意,Christoph Gohlke 为所有 Windows 平台的流行 Python 包提供二进制文件http://www.lfd.uci.edu/~gohlke/pythonlibs/

        事实上,构建一些 Python 包需要 C 编译器(例如 mingw32)和依赖项的库头文件。这在 Windows 上可能是一场噩梦,所以请记住 Christoph Gohlke 这个名字。

        【讨论】:

          【解决方案10】:

          我在 Windows 上安装软件包时遇到问题。找到了解决方案。它适用于 Windows7+。主要是 Windows Powershell 的任何东西都应该能够使其工作。 This 可以帮助你开始使用它。

          • 首先,您需要将 python 安装添加到您的 PATH 变量中。 This 应该会有所帮助。
          • 您需要以 zip 格式下载您尝试安装的软件包并解压缩。如果是一些奇怪的 zip 格式,请使用 7Zip,它应该被解压缩。
          • 使用 Windows Powershell 导航到使用 setup.py 提取的目录(如果遇到问题,请使用链接)
          • 运行命令python setup.py install

          当没有其他任何意义时,这对我有用。我使用 Python 2.7,但文档表明这同样适用于 Python 3.x。

          【讨论】:

            【解决方案11】:

            This 是一个关于如何在 Windows 上获取easy_install 的好教程。简短的回答:将C:\Python26\Scripts(或您安装的任何python)添加到您的PATH。

            【讨论】:

            • 第 1 步 - 将 python 识别为命令 第 2 步 - 运行 setup.py 文件
            • 这个答案已经过时了。请更改它。
            • 在 Windows 上安装 Python 3.7 时,有一个不错的选项可以将该路径添加到 PATH 环境变量中。
            【解决方案12】:

            您也可以只下载并运行 ez_setup.py,尽管 SetupTools 文档不再建议这样做。就在 2 周前对我来说还不错。

            【讨论】:

              猜你喜欢
              • 2016-08-01
              • 2015-02-19
              • 2018-04-13
              • 2018-12-12
              相关资源
              最近更新 更多