Installing Python modules¶
As a popular open source development project, Python has an active supporting community of contributors and users that also make their software available for other Python developers to use under open-source license terms.
這樣可以讓 Python 使用者們有效地共享和合作,並受益於其他人對常見(有時甚至是罕見)的問題已經建立的解決方案,更可以在公用社群中潛在地貢獻他們自己的解決方案。
這份指南涵蓋了上述過程中的安裝部分。如果是要建立及分享你自己的 Python 專案,請參考 Python packaging user guide。
備註
對於企業和其他機構的使用者,要注意到,許多組織對於使用和貢獻開源軟體都有自己的政策。在開始使用配備 Python 的發布及安裝工具時,請將那些政策納入考量。
關鍵術語¶
pip is the preferred installer program. It is included by default with the Python binary installers.
virtual environment(虛擬環境)是一種半隔離的 Python 環境,可以為某個特定應用程式安裝其所需的套件,而不用在整個系統上安裝它們。
venvis the standard tool for creating virtual environments. It defaults to installing pip into all created virtual environments.virtualenvis a third-party alternative (and predecessor) tovenv.The Python Package Index (PyPI) is a public repository of open source licensed packages made available for use by other Python users.
The Python Packaging Authority is the group of developers and documentation authors responsible for the maintenance and evolution of the standard packaging tools and the associated metadata and file format standards. They maintain a variety of tools, documentation, and issue trackers on GitHub.
在 3.5 版的變更: 對於建立虛擬環境,現在推薦使用 venv。
基本用法¶
標準封裝工具皆是以能從命令列使用的方式被設計的。
The following command will install the latest version of a module and its dependencies from PyPI:
python -m pip install SomePackage
備註
對於 POSIX 使用者(包括 macOS 和 Linux 使用者),本指南中的範例皆假設有使用 virtual environment。
對於 Windows 使用者,本指南中的範例皆假設在安裝 Python 時,「可調整系統 PATH 環境變數」的選項已被選取。
在命令列中直接指定一個明確的或最小的版本也是可行的。當使用像是 >、< 的比較運算子,或某些可被 shell 所解釋的其他特殊字元時,套件名稱與版本編號應該要放在雙引號內:
python -m pip install SomePackage==1.0.4 # 明確版本
python -m pip install "SomePackage>=1.0.4" # 最小版本
通常,如果一個合適的模組已被安裝,嘗試再次安裝它將不會有任何效果。要升級現有的模組就必須明確地請求:
python -m pip install --upgrade SomePackage
More information and resources regarding pip and its capabilities can be found in the Python Packaging User Guide.
虛擬環境的建立是使用 venv 模組來完成。要在一個已啟用的虛擬環境中安裝套件,可使用前面展示的指令。
我該如何...?¶
接下來是關於一些常見任務的快速解答或連結。
...只為目前的使用者安裝套件?¶
把 --user 選項傳給 python -m pip install,這樣將會只為目前使用者而非系統的所有使用者安裝套件。
...安裝科學的 Python 套件?¶
A number of scientific Python packages have complex binary dependencies, and aren't currently easy to install using pip directly. It will often be easier for users to install these packages by other means rather than attempting to install them with pip.
...平行安裝多個 Python 版本並使用它們?¶
On Linux, macOS, and other POSIX systems, use the versioned Python commands
in combination with the -m switch to run the appropriate copy of
pip:
python3 -m pip install SomePackage # default Python 3
python3.14 -m pip install SomePackage # specifically Python 3.14
Appropriately versioned pip commands may also be available.
On Windows, use the py Python launcher in combination with the -m
switch:
py -3 -m pip install SomePackage # default Python 3
py -3.14 -m pip install SomePackage # specifically Python 3.14
常見的安裝問題¶
在 Linux 上安裝套件至系統 Python¶
On Linux systems, a Python installation will typically be included as part of the distribution. Installing into this Python installation requires root access to the system, and may interfere with the operation of the system package manager and other components of the system if a component is unexpectedly upgraded using pip.
On such systems, it is often better to use a virtual environment or a per-user installation when installing packages with pip.
未安裝 pip¶
It is possible that pip does not get installed by default. One potential fix is:
python -m ensurepip --default-pip
There are also additional resources for installing pip.
安裝二進制擴充 (binary extension)¶
Python once relied heavily on source-based distribution, with end users being expected to compile extension modules from source as part of the installation process.
With the introduction of the binary wheel format, and the ability to publish wheels through PyPI, this problem is diminishing, as users are more regularly able to install pre-built extensions rather than needing to build them themselves.
Some of the solutions for installing scientific software that are not yet available as pre-built wheel files may also help with obtaining other binary extensions without needing to build them locally.