文章目錄
- 中文翻譯
- 安裝 Python 模塊
- 關鍵術語
- 基本使用
- 我應如何 ...?
- ... 在 Python 3.4 之前的 Python 版本中安裝 `pip` ?
- ... 只為當前用戶安裝軟件包?
- ... 安裝科學計算類 Python 軟件包?
- ... 使用並行安裝的多個 Python 版本?
- 常見的安裝問題
- 原文
- Installing Python Modules
中文翻譯
安裝 Python 模塊
作為一個流行的開源開發項目,Python擁有一個活躍的貢獻者和用戶支持社區,這些社區也可以讓他們的軟件可供其他Python開發人員在開源許可條款下使用。
這允許Python用戶有效地共享和協作,從其他人已經創建的解決方案中受益於常見(有時甚至是罕見的)問題,以及可以提供他們自己的解決方案。
本指南涵蓋了分發部分的流程。有關安裝其他Python項目的指南,請參閱 安裝指南。
注解
對於企業和其他機構用戶,請注意許多組織都有自己的政策來使用和貢獻開源軟件。在使用Python提供的分發和安裝工具時,請考慮這些政策。
關鍵術語
pip
是首選的安裝程序。從Python 3.4開始,它默認包含在Python二進制安裝程序中。- virtual environment 是一種半隔離的 Python 環境,允許為特定的應用安裝各自的包,而不是安裝到整個系統。
venv
是創建虛擬環境的標准工具,從 Python 3.3 開始成為 Python 的組成部分。 從 Python 3.4 開始,它會默認安裝pip
到所創建的全部虛擬環境。virtualenv
是venv
的第三方替代(及其前身)。 它允許在 Python 3.4 之前的版本中使用虛擬環境,那些版本或是完全不提供venv
,或是不會自動安裝pip
到所創建的虛擬環境。- Python Packaging Index 是一個由 Python 用戶向其他用戶發布開源許可軟件包的公共倉庫。
- Python Packaging Authority 是負責標准打包工具以及相關元數據和文件格式標准維護與改進的開發人員和文檔作者團隊。 他們基於 GitHub 和 Bitbucket 這兩個平台維護着各種工具、文檔和問題追蹤系統。
distutils
是最初的構建和分發系統,於 1998 年首次加入 Python 標准庫。 雖然直接使用distutils
的方式已被淘汰,它仍然是當前打包和分發架構的基礎,而且它不僅仍然是標准庫的一部分,這個名稱還以其他方式存在(例如用於協調 Python 打包標准開發流程的郵件列表就以此命名)。
在 3.5 版更改: 現在推薦使用 venv
來創建虛擬環境。
參見
基本使用
標准打包工具完全是針對命令行使用方式來設計的。
以下命令將從 Python Packaging Index 安裝一個模塊的最新版本及其依賴項:
python -m pip install SomePackage
注解
對於 POSIX 用戶(包括 Mac OS X 和 Linux 用戶)本指南中的示例假定使用了 virtual environment。
對於 Windows 用戶,本指南中的示例假定在安裝 Python 時選擇了修改系統 PATH 環境變量。
在命令行中指定一個准確或最小版本也是可以的。 當使用比較運算符例如 >
, <
或其他某些可以被終端所解析的特殊字符時,包名稱與版本號應當用雙引號括起來:
python -m pip install SomePackage==1.0.4 # specific version
python -m pip install "SomePackage>=1.0.4" # minimum version
通常,如果一個匹配的模塊已安裝,嘗試再次安裝將不會有任何效果。 要升級現有模塊必須顯式地發出請求:
python -m pip install --upgrade SomePackage
更多有關 pip
及其功能的信息和資源可以在 Python 軟件包用戶指南 中找到。
虛擬環境的創建可使用 venv
模塊來完成。 向已激活虛擬環境安裝軟件包可使用上文所介紹的命令。
參見
我應如何 …?
這是一些常見任務的快速解答或相關鏈接。
… 在 Python 3.4 之前的 Python 版本中安裝 pip
?
Python 捆綁 pip
是從 Python 3.4 才開始的。 對於更早的版本,pip
需要“引導安裝bootstrapped”,具體說明參見 Python 軟件包用戶指南。
參見
… 只為當前用戶安裝軟件包?
將 --user
選項傳入 python -m pip install
將只為當前用戶而非為系統中的所有用戶安裝軟件包。
… 安裝科學計算類 Python 軟件包?
許多科學計算類 Python 軟件包都有復雜的二進制編譯文件依賴,直接使用 pip
安裝目前並不太容易。 在當前情況下,通過 其他方式 而非嘗試用 pip
安裝這些軟件包對用戶來說通常會更容易。
參見
… 使用並行安裝的多個 Python 版本?
在 Linux, Mac OS X 以及其他 POSIX 系統中,使用帶版本號的 Python 命令配合 -m
開關選項來運行特定版本的 pip
:
python2 -m pip install SomePackage # default Python 2
python2.7 -m pip install SomePackage # specifically Python 2.7
python3 -m pip install SomePackage # default Python 3
python3.4 -m pip install SomePackage # specifically Python 3.4
也可以使用帶特定版本號的 pip
命令。
在 Windows 中,使用 py
Python 啟動器命令配合 -m
開關選項:
py -2 -m pip install SomePackage # default Python 2
py -2.7 -m pip install SomePackage # specifically Python 2.7
py -3 -m pip install SomePackage # default Python 3
py -3.4 -m pip install SomePackage # specifically Python 3.4
常見的安裝問題
在 Linux 的系統 Python 版本上安裝
Linux 系統通常會將某個 Python 版本作為發行版的一部分包含在內。 將軟件包安裝到這個 Python 版本上需要系統 root 權限,並可能會干擾到系統包管理器和其他系統組件的運作,如果這些組件在使用 pip
時被意外升級的話。
在這樣的系統上,通過 pip
安裝軟件包通常最好是使用虛擬環境或分用戶安裝。
未安裝 pip
默認情況下可能未安裝 pip
,一種可選解決方案是:
python -m ensurepip --default-pip
還有其他資源可用來 安裝 pip
安裝二進制編譯擴展
Python 通常非常依賴基於源代碼的發布方式,也就是期望最終用戶在安裝過程中使用源碼來編譯生成擴展模塊。
隨着對二進制碼 wheel
格式支持的引入,以及通過 Python Packaging Index 至少發布 Windows 和 Mac OS X 版的 wheel 文件,預計此問題將逐步得到解決,因為用戶將能夠更頻繁地安裝預編譯擴展,而不再需要自己編譯它們。
某些用來安裝 科學計算類軟件包 的解決方案對於尚未提供預編譯 wheel
文件的那些擴展模塊來說,也有助於用戶在無需進行本機編譯的情況下獲取二進制碼擴展模塊。
原文
Installing Python Modules
-
Email
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.
This allows Python users to share and collaborate effectively, benefiting from the solutions others have already created to common (and sometimes even rare!) problems, as well as potentially contributing their own solutions to the common pool.
This guide covers the installation part of the process. For a guide to creating and sharing your own Python projects, refer to the distribution guide.
Note
For corporate and other institutional users, be aware that many organisations have their own policies around using and contributing to open source software. Please take such policies into account when making use of the distribution and installation tools provided with Python.
Key terms
pip
is the preferred installer program. Starting with Python 3.4, it is included by default with the Python binary installers.- A virtual environment is a semi-isolated Python environment that allows packages to be installed for use by a particular application, rather than being installed system wide.
venv
is the standard tool for creating virtual environments, and has been part of Python since Python 3.3. Starting with Python 3.4, it defaults to installingpip
into all created virtual environments.virtualenv
is a third party alternative (and predecessor) tovenv
. It allows virtual environments to be used on versions of Python prior to 3.4, which either don’t providevenv
at all, or aren’t able to automatically installpip
into created environments.- The Python Packaging Index 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 both GitHub and Bitbucket.
distutils
is the original build and distribution system first added to the Python standard library in 1998. While direct use ofdistutils
is being phased out, it still laid the foundation for the current packaging and distribution infrastructure, and it not only remains part of the standard library, but its name lives on in other ways (such as the name of the mailing list used to coordinate Python packaging standards development).
Changed in version 3.5: The use of venv
is now recommended for creating virtual environments.
See also
Python Packaging User Guide: Creating and using virtual environments
Basic usage
The standard packaging tools are all designed to be used from the command line.
The following command will install the latest version of a module and its dependencies from the Python Packaging Index:
python -m pip install SomePackage
Note
For POSIX users (including Mac OS X and Linux users), the examples in this guide assume the use of a virtual environment.
For Windows users, the examples in this guide assume that the option to adjust the system PATH environment variable was selected when installing Python.
It’s also possible to specify an exact or minimum version directly on the command line. When using comparator operators such as >
, <
or some other special character which get interpreted by shell, the package name and the version should be enclosed within double quotes:
python -m pip install SomePackage==1.0.4 # specific version
python -m pip install "SomePackage>=1.0.4" # minimum version
Normally, if a suitable module is already installed, attempting to install it again will have no effect. Upgrading existing modules must be requested explicitly:
python -m pip install --upgrade SomePackage
More information and resources regarding pip
and its capabilities can be found in the Python Packaging User Guide.
Creation of virtual environments is done through the venv
module. Installing packages into an active virtual environment uses the commands shown above.
See also
Python Packaging User Guide: Installing Python Distribution Packages
How do I …?
These are quick answers or links for some common tasks.
… install pip
in versions of Python prior to Python 3.4?
Python only started bundling pip
with Python 3.4. For earlier versions, pip
needs to be “bootstrapped” as described in the Python Packaging User Guide.
See also
Python Packaging User Guide: Requirements for Installing Packages
… install packages just for the current user?
Passing the --user
option to python -m pip install
will install a package just for the current user, rather than for all users of the system.
… install scientific Python packages?
A number of scientific Python packages have complex binary dependencies, and aren’t currently easy to install using pip
directly. At this point in time, it will often be easier for users to install these packages by other means rather than attempting to install them with pip
.
See also
Python Packaging User Guide: Installing Scientific Packages
… work with multiple versions of Python installed in parallel?
On Linux, Mac OS X, and other POSIX systems, use the versioned Python commands in combination with the -m
switch to run the appropriate copy of pip
:
python2 -m pip install SomePackage # default Python 2
python2.7 -m pip install SomePackage # specifically Python 2.7
python3 -m pip install SomePackage # default Python 3
python3.4 -m pip install SomePackage # specifically Python 3.4
Appropriately versioned pip
commands may also be available.
On Windows, use the py
Python launcher in combination with the -m
switch:
py -2 -m pip install SomePackage # default Python 2
py -2.7 -m pip install SomePackage # specifically Python 2.7
py -3 -m pip install SomePackage # default Python 3
py -3.4 -m pip install SomePackage # specifically Python 3.4
Common installation issues
Installing into the system Python on Linux
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 not installed
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.
Installing binary extensions
Python has typically 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 support for the binary wheel
format, and the ability to publish wheels for at least Windows and Mac OS X through the Python Packaging Index, this problem is expected to diminish over time, 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.