如何安裝 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