如何在 Windows 上安装 Python 包?
- 2025-01-22 08:45:00
- admin 原创
- 74
问题描述:
我在设置 Python 包时遇到了困难。SetupTools 中的 EasyInstall应该可以解决这个问题,但是它们没有 Python 2.6 的可执行文件。
例如,要安装 Mechanize,我只需根据 INSTALL.txt 将 Mechanize 文件夹放在 C:\Python24\Lib\site-packages 中,但运行测试不起作用。有人能帮我解释一下吗?谢谢!
解决方案 1:
接受的答案已经过时了。因此,首先,pip
优于easy_install
,(为什么使用 pip 而不是 easy_install?)。然后按照以下步骤在 Windows 上安装pip
,这很容易。
安装
setuptools
:
curl https://bootstrap.pypa.io/ez_setup.py | python
安装
pip
:
curl https://bootstrap.pypa.io/get-pip.py | python
或者,您可以将路径添加到您的环境中,以便在
pip
任何地方使用。它位于类似 的地方C:Python33Scripts
。
解决方案 2:
Windows 版 Python 的较新版本附带pip包管理器。(来源)
如果你使用的是 Python 2 >=2.7.9 或 Python 3 >=3.4,则 pip 已安装
使用它来安装包:
cd C:PythonScripts\npip.exe install <package-name>
因此,对于你来说,情况是这样的:
pip.exe install mechanize
解决方案 3:
这是关于如何easy_install
在 Windows 上安装的一个很好的教程。简短的回答:将C:Python26Scripts
(或任何您已安装的 Python)添加到您的 PATH。
解决方案 4:
您不需要 setuptools 的可执行文件。您可以下载源代码,解压,遍历下载的目录并 python setup.py install
在命令提示符中运行
解决方案 5:
从 Python 2.7 开始,pip 已默认包含在内。只需通过以下方式下载所需的软件包即可
python -m pip install [package-name]
解决方案 6:
正如我在其他地方所写
Python 中的打包问题非常严重。根本原因是该语言没有附带包管理器。
幸运的是,Python 有一个包管理器,叫做Pip。Pip 的灵感来自 Ruby 的 Gem,但缺少一些功能。讽刺的是,Pip 本身的安装很复杂。在流行的 64 位 Windows 上安装需要从源代码构建和安装两个包。对于任何编程新手来说,这都是一个很大的要求。
因此正确的做法是安装 pip。但是如果您懒得安装,Christoph Gohlke 为所有 Windows 平台提供了流行的 Python 包的二进制文件http://www.lfd.uci.edu/~gohlke/pythonlibs/
事实上,构建一些 Python 包需要 C 编译器(例如 mingw32)和依赖项的库头文件。这在 Windows 上可能是一场噩梦,所以请记住 Christoph Gohlke 这个名字。
解决方案 7:
通过命令提示符升级 pip(Python 目录)
D:Python 3.7.2>python -m pip install --upgrade pip
现在您可以安装所需的模块
D:Python 3.7.2>python -m pip install <<yourModuleName>>
解决方案 8:
我在 Windows 上安装软件包时遇到了问题。找到了解决方案。它适用于 Windows7+。基本上,任何带有 Windows Powershell 的东西都应该能够使它工作。这可以帮助您开始使用它。
首先,您需要将 Python 安装添加到 PATH 变量中。这应该会有所帮助。
您需要下载要安装的 zip 格式的软件包并解压。如果是某些奇怪的 zip 格式,请使用 7Zip,然后应可将其解压。
使用 Windows Powershell 导航到使用 setup.py 解压的目录(如果遇到问题,请使用链接)
运行命令
python setup.py install
当其他方法都不起作用时,这种方法对我来说很管用。我使用的是 Python 2.7,但文档表明这种方法也适用于 Python 3.x。
解决方案 9:
pip 是 python 的软件包安装程序,请先更新它,然后下载所需的内容
python -m pip install --upgrade pip
然后:
python -m pip install <package_name>
解决方案 10:
您也可以直接下载并运行 ez_setup.py,尽管 SetupTools 文档不再建议这样做。就在 2 周前,它对我来说还运行良好。
解决方案 11:
PS D:simcut> C:Python27Scriptspip.exe install networkx
Collecting networkx
c:python27libsite-packagespip_vendor
equestspackages/urllib3/utilssl_.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:python27libsite-packagespip_vendor
equestspackages/urllib3/utilssl_.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:python27libsite-packagespip_vendor
equestspackages/urllib3/utilssl_.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 可执行文件即可。
解决方案 12:
正如 Blauhirn 所说,pip 2.7 已预装。如果它不工作,可能需要将其添加到路径中。
但是,如果您运行的是 Windows 10,则不再需要打开终端来安装模块。打开 Python 也是如此。
您可以直接在搜索菜单中输入pip install mechanize
,选择命令,它将安装:
但是,如果出现任何问题,它可能会在您读取错误之前关闭,但它仍然是一个有用的快捷方式。