欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Installing packages using pip and virtual environments

程序员文章站 2022-06-27 20:29:46
https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/ Installing packages using pip and virtual environments This guide d ......

 

installing packages using pip and virtual environments

this guide discusses how to install packages using  and a virtual environment manager: either  for python 3 or  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.

note

 

this doc uses the term package to refer to a distribution package which is different from a import package that which is used to import modules in your python source code.

installing 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.

windows

the python installers for windows include pip. you should be able to access pip using:

py -m pip --version
pip 9.0.1 from c:\python36\lib\site-packages (python 3.6.1)

you can make sure that pip is up-to-date by running:

py -m pip install --upgrade pip

linux and macos

debian and most other distributions include a  package, if you want to use the linux distribution-provided versions of pip see installing pip/setuptools/wheel with linux package managers.

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

afterwards, you should have the newest pip installed in your user site:

python3 -m pip --version
pip 9.0.1 from $home/.local/lib/python3.6/site-packages (python 3.6)

installing virtualenv

note

 

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.

 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.

on macos and linux:

python3 -m pip install --user virtualenv

on windows:

py -m pip install --user virtualenv

creating a virtual environment

 (for python 3) and  (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.

to create a virtual environment, go to your project’s directory and run venv. if you are using python 2, replace venv with virtualenv in the below commands.

on macos and linux:

python3 -m venv env

on windows:

py -m venv env

the second argument is the location to create the virtual environment. generally, you can just create this in your project and call it env.

venv will create a virtual python installation in the env folder.

note

 

you should exclude your virtual environment directory from your version control system using .gitignore or similar.

activating a virtual environment

before you can start installing or using packages in your virtual environment you’ll need to activate it. activating a virtual environment will put the virtual environment-specific python and pip executables into your shell’s path.

on macos and linux:

source env/bin/activate

on windows:

.\env\scripts\activate

you can confirm you’re in the virtual environment by checking the location of your python interpreter, it should point to the env directory.

on macos and linux:

which python
.../env/bin/python

on windows:

where python
.../env/bin/python.exe

as long as your virtual environment is activated pip will install packages into that specific environment and you’ll be able to import and use packages in your python application.

leaving the virtual environment

if you want to switch projects or otherwise leave your virtual environment, simply run:

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.

installing packages

now that you’re in your virtual environment you can install packages. let’s install the requests library from the python package index (pypi):

pip install requests

pip should download requests and all of its dependencies and install them:

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

installing specific versions

pip allows you to specify which version of a package to install using . for example, to install a specific version of requests:

pip install requests==2.18.4

to install the latest 2.x release of requests:

pip install requests>=2.0.0,<3.0.0

to install pre-release versions of packages, use the --pre flag:

pip install --pre requests

installing extras

some packages have optional . you can tell pip to install these by specifying the extra in brackets:

pip install requests[security]

installing from source

pip can install a package directly from source, for example:

cd google-auth
pip install .

additionally, pip can install packages from source in , meaning that changes to the source directory will immediately affect the installed package without needing to re-install:

pip install --editable .

installing from version control systems

pip can install packages directly from their version control system. for example, you can install directly from a git repository:

git+https://github.com/googlecloudplatform/google-auth-library-python.git#egg=google-auth

for more information on supported version control systems and syntax, see pip’s documentation on vcs support.

installing from local archives

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:

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:

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.

using other package indexes

if you want to download packages from a different index than the python package index (pypi), you can use the --index-url flag:

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:

pip install --extra-index-url http://index.example.com/simple/ someproject

upgrading packages

pip can upgrade packages in-place using the --upgrade flag. for example, to install the latest version of requests and all of its dependencies:

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:

pip install -r requirements.txt

freezing dependencies

pip can export a list of all installed packages and their versions using the freeze command:

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.