Installing packages using pip and virtual environments¶
This guide discusses how to install packages using pip and a virtual environment manager: either venv for Python 3 or virtualenv for Python 2. These are the lowest-level tools for managing Python packages and are recommended if higher-level tools do not suit your needs.
备注
This doc uses the term package to refer to a Distribution Package which is different from an Import Package that which is used to import modules in your Python source code.
Installing pip¶
pip is the reference Python package manager. It’s used to install and update packages. You’ll need to make sure you have the latest version of pip installed.
Debian and most other distributions include a python-pip package; if you want to use the Linux distribution-provided versions of pip, see 用 Linux 软件包管理器安装 pip/setuptools/wheel.
You can also install pip yourself to ensure you have the latest version. It’s recommended to use the system pip to bootstrap a user installation of pip:
python3 -m pip install --user --upgrade pip
python3 -m pip --version
Afterwards, you should have the latest version of pip installed in your user site:
pip 21.1.3 from $HOME/.local/lib/python3.9/site-packages (python 3.9)
The Python installers for Windows include pip. You can make sure that pip is up-to-date by running:
py -m pip install --upgrade pip
py -m pip --version
Afterwards, you should have the latest version of pip:
pip 21.1.3 from c:\python39\lib\site-packages (Python 3.9.4)
安装 virtualenv¶
备注
If you are using Python 3.3 or newer, the venv
module is
the preferred way to create and manage virtual environments.
venv is included in the Python standard library and requires no additional installation.
If you are using venv, you may skip this section.
virtualenv is used to manage Python packages for different projects. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects. You can install virtualenv using pip.
python3 -m pip install --user virtualenv
py -m pip install --user virtualenv
创建一个虚拟环境¶
venv (for Python 3) and virtualenv (for Python 2) allow you to manage separate package installations for different projects. They essentially allow you to create a “virtual” isolated Python installation and install packages into that virtual installation. When you switch projects, you can simply create a new virtual environment and not have to worry about breaking the packages installed in the other environments. It is always recommended to use a virtual environment while developing Python applications.
要创建一个虚拟环境,进入您的项目目录并运行 venv 。如果您正在使用 Python2,在下面的命令中把 venv
改为 virtualenv
。
python3 -m venv env
py -m venv env
第二个参数是创建虚拟环境的位置。一般来说,您可以直接在您的项目中创建它,并称之为 env
。
venv 将在 env
文件夹中创建一个虚拟的 Python 安装。
备注
您应该使用 .gitignore
或类似的方法将您的虚拟环境目录从你的版本控制系统中排除。
激活一个虚拟环境¶
在您开始安装或使用虚拟环境中的软件包之前,你需要 激活 它。激活虚拟环境将把虚拟环境专用的 python
和 pip
可执行文件放入你的 shell 的 PATH
。
source env/bin/activate
.\env\Scripts\activate
You can confirm you’re in the virtual environment by checking the location of your Python interpreter:
which python
where python
It should be in the env
directory:
.../env/bin/python
...\env\Scripts\python.exe
只要您的虚拟环境被激活,pip 就会将软件包安装到该特定环境中,您就可以在您的 Python 应用程序中导入和使用软件包。
离开虚拟环境¶
如果您想切换项目或以其他方式离开你的虚拟环境,只需运行:
deactivate
If you want to re-enter the virtual environment just follow the same instructions above about activating a virtual environment. There’s no need to re-create the virtual environment.
安装软件包¶
现在您在您的虚拟环境中,您可以安装软件包。让我们从 Python Package Index (PyPI) 中安装 Requests 库:
python3 -m pip install requests
py -m pip install requests
pip应该下载 request 及其所有的依赖项并安装它们:
Collecting requests
Using cached requests-2.18.4-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Using cached chardet-3.0.4-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21.1 (from requests)
Using cached urllib3-1.22-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
Using cached certifi-2017.7.27.1-py2.py3-none-any.whl
Collecting idna<2.7,>=2.5 (from requests)
Using cached idna-2.6-py2.py3-none-any.whl
Installing collected packages: chardet, urllib3, certifi, idna, requests
Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
安装特定版本¶
pip 允许您使用 版本说明符 来指定安装哪个版本的软件包。例如,要安装一个特定版本的 requests
:
python3 -m pip install requests==2.18.4
py -m pip install requests==2.18.4
要安装最新的 2.x
版本的 requests:
python3 -m pip install requests>=2.0.0,<3.0.0
py -m pip install requests>=2.0.0,<3.0.0
要安装预发布(pre-release)版本的软件包,请使用 --pre
标志:
python3 -m pip install --pre requests
py -m pip install --pre requests
安装附加功能¶
有些软件包有可选的 额外功能 。你可以通过在括号中指定额外的东西来告诉 pip 安装这些东西:
python3 -m pip install requests[security]
py -m pip install requests[security]
从源文件安装¶
pip 可以直接从源代码安装一个软件包,例如:
cd google-auth
python3 -m pip install .
cd google-auth
py -m pip install .
Additionally, pip can install packages from source in development mode, meaning that changes to the source directory will immediately affect the installed package without needing to re-install:
python3 -m pip install --editable .
py -m pip install --editable .
从版本控制系统进行安装¶
pip 可以直接从他们的版本控制系统中安装软件包。例如,你可以直接从 git 仓库中安装:
git+https://github.com/GoogleCloudPlatform/google-auth-library-python.git#egg=google-auth
关于支持的版本控制系统和语法的更多信息,请参见 pip 的文档:VCS Support 。
从本地档案库安装¶
If you have a local copy of a Distribution Package’s archive (a zip, wheel, or tar file) you can install it directly with pip:
python3 -m pip install requests-2.18.4.tar.gz
py -m pip install requests-2.18.4.tar.gz
If you have a directory containing archives of multiple packages, you can tell pip to look for packages there and not to use the Python Package Index (PyPI) at all:
python3 -m pip install --no-index --find-links=/local/dir/ requests
py -m pip install --no-index --find-links=/local/dir/ requests
This is useful if you are installing packages on a system with limited connectivity or if you want to strictly control the origin of distribution packages.
使用其他软件包的索引¶
如果您想从不同的索引下载软件包,而不是 Python 包索引(PyPI) ,您可以使用 --index-url
标志:
python3 -m pip install --index-url http://index.example.com/simple/ SomeProject
py -m pip install --index-url http://index.example.com/simple/ SomeProject
If you want to allow packages from both the Python Package Index (PyPI)
and a separate index, you can use the --extra-index-url
flag instead:
python3 -m pip install --extra-index-url http://index.example.com/simple/ SomeProject
py -m pip install --extra-index-url http://index.example.com/simple/ SomeProject
升级包¶
pip can upgrade packages in-place using the --upgrade
flag. For example, to
install the latest version of requests
and all of its dependencies:
python3 -m pip install --upgrade requests
py -m pip install --upgrade requests
Using requirements files¶
Instead of installing packages individually, pip allows you to declare all
dependencies in a Requirements File. For
example you could create a requirements.txt
file containing:
requests==2.18.4
google-auth==1.1.0
And tell pip to install all of the packages in this file using the -r
flag:
python3 -m pip install -r requirements.txt
py -m pip install -r requirements.txt
Freezing dependencies¶
Pip can export a list of all installed packages and their versions using the
freeze
command:
python3 -m pip freeze
py -m pip freeze
Which will output a list of package specifiers such as:
cachetools==2.0.1
certifi==2017.7.27.1
chardet==3.0.4
google-auth==1.1.1
idna==2.6
pyasn1==0.3.6
pyasn1-modules==0.1.4
requests==2.18.4
rsa==3.4.2
six==1.11.0
urllib3==1.22
This is useful for creating Requirements Files that can re-create the exact versions of all packages installed in an environment.