如何安装 Python 模块?
包含 Python 定义和语句的文件称为模块。模块是一个包含 Python 代码的文件;“Program.py”文件将是一个名为“Program”的模块。我们利用模块将复杂的程序分解成更小、更易于管理的部分。代码重用也可以通过模块实现。我们可以将我们最常用的函数定义在一个模块中并导入它,而不是将其定义复制到多个程序中。
在 Windows 中安装 Python 模块
Python 包管理器 (pip) 允许安装模块和包。打开终端并使用 pip 命令在整个系统中安装模块。
在 Python 中使用 PIP
PIP 是 Python 模块或包的包管理器。PIP 内置于 Python 3.4 及更高版本中。
示例
使用以下命令从 Python 包索引安装模块的最新版本以及任何依赖项:
C:\Users\Lenovo>pip install hashlib
输出
以下是安装任何模块后的输出:
Collecting hashlib
Downloading hashlib-20081119.zip (42 kB)
---------------------------------------- 42.3/42.3 KB 2.1 MB/s eta 0:00:00
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [7 lines of output]
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "<pip-setuptools-caller>", line 34, in <module>
File "C:\Users\Lenovo\AppData\Local\Temp\pip-install-nhi53g8x\hashlib_9d646b53cf6c4df592a92567c02655dc\setup.py", line 68
print "unknown OS, please update setup.py"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
WARNING: You are using pip version 22.0.4; however, version 22.2.2 is available.
You should consider upgrading via the 'C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\python.exe -m pip install --upgrade pip' command.
注意 - 这将自动安装 Python 模块。通常,您使用虚拟环境或 venv,而不是在整个计算机上安装模块。
您需要安装 pip 才能使其正常工作。安装过程取决于平台。
需要 Python 3.4 或更高版本,因为该版本包含 pip。
示例
如果 pip 不可访问且您使用的是 Python 3.4 或更高版本,请运行以下命令:
C:\Users\Lenovo>py -3 -m ensurepip
输出
以下是上述代码的输出
Looking in links: c:\Users\Lenovo\AppData\Local\Temp\tmpvdjjyjwx Requirement already satisfied: setuptools in c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages (58.1.0) Requirement already satisfied: pip in c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages (22.0.4)
检查 PIP 版本
在您的命令行中找到 Python 的脚本目录,然后键入以下内容以验证您的版本:
C:\Users\Lenovo>python --version
输出如下:
Python 3.10.5
注意
您可以使用 pip 安装 python 包。例如,要安装最新版本的“SomeProject”:
$ pip install 'SomeProject'
要安装特定版本,请使用以下命令
$ pip install 'SomeProject==1.4'
要安装大于或等于一个版本且小于另一个版本的版本,请使用以下命令:
$ pip install 'SomeProject>=1,<2'
在 Python 中使用 Conda
Conda 是一个开源的包管理和环境管理系统,可在 Windows、macOS、Linux 和 z/OS 上运行。Conda 可以快速安装、运行和更新包以及它们对包的依赖项。
Conda 使在您自己的计算机上创建、保存、加载和在环境之间切换变得容易。它旨在打包和分发 Python 程序,但它可以对任何语言的软件执行相同的操作。
您可以借助 Conda 作为包管理来搜索和安装包。Conda 是一个环境管理器,因此如果您需要一个需要不同 Python 版本的包,则无需切换到另一个环境管理器。
搜索包
对于后续步骤,请使用 Anaconda Prompt 或终端检查特定包(例如“hiredis”)是否可用于安装:
(base) C:\Users\Lenovo>conda search hiredis
输出
以下是上述命令的输出:
Loading channels: done # Name Version Build Channel hiredis 1.1.0 py310h2bbff1b_1 pkgs/main hiredis 1.1.0 py36h2bbff1b_1 pkgs/main hiredis 1.1.0 py37h2bbff1b_1 pkgs/main hiredis 1.1.0 py38h2bbff1b_1 pkgs/main hiredis 1.1.0 py39h2bbff1b_1 pkgs/main hiredis 2.0.0 py310h2bbff1b_0 pkgs/main hiredis 2.0.0 py37h2bbff1b_0 pkgs/main hiredis 2.0.0 py38h2bbff1b_0 pkgs/main hiredis 2.0.0 py39h2bbff1b_0 pkgs/main
安装包
使用 Anaconda Prompt 或终端安装特定包(例如“hiredis”):
(base) C:\Users\Lenovo>conda install hiredis
输出
以下是上述命令的输出:
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: C:\Users\Lenovo\anaconda3
added / updated specs:
- hiredis
The following packages will be downloaded:
package | build
------------------------|-----------------
conda-4.14.0 | py39haa95532_0 937 KB
hiredis-2.0.0 | py39h2bbff1b_0 23 KB
----------------------------------------------------------
Total: 959 KB
The following NEW packages will be INSTALLED:
hiredis pkgs/main/win-64::hiredis-2.0.0-py39h2bbff1b_0
The following packages will be UPDATED:
conda 4.12.0-py39haa95532_0 --> 4.14.0-py39haa95532_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
hiredis-2.0.0 | 23 KB | ############################################################################ | 100%
conda-4.14.0 | 937 KB | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP