Python Programming/Package management
pip is the standard Python package manager, making it easy to download and install packages from the PyPI repository. pip seems to be part of Python distribution since Python 2.7.9 and 3.5.4. If you do not have pip, you can install it by downloading get-pip.py from bootstrap.pypa.io and running python get-pip.py. Other package managers are not covered in this chapter.
Examples of pip use:
- pip install xlrd
- Installs xlrd package from the PyPI repository, or from another repository if customized to search in additional repositories.
- pip install --upgrade xlrd
- Upgrades a package to the latest version.
- pip install mypackage.whl
- Installs the package from wheel file mypackage.whl. This is useful when for whatever reason installation from PyPI fails and you need to download the wheel file (.whl) of the package manually.
- pip freeze
- Lists installed packages and their versions.
- pip show xlrd
- Outputs information about an installed package (here xlrd), including version, author and license.
- python -m pip install xlrd
- Calls pip via python and -m option. Useful e.g. for installing packages for PyPy (a just-in-time compiler for Python), in which case you use pypy -m pip install xlrd.
- pip --version
- Outputs the pip version.
- pip install --upgrade pip
- Upgrades pip itself.
PyPI is an online repository of Python packages, many of which are published under a rather permissive license such as MIT license or one of the BSD licenses. PyPI hosts both pure-Python packages and Python packages taking advantage of the C language. Installing pure-Python packages such as xlrd is usually seamless. As for C language packages, many of them have precompiled binaries for multiple operating systems, making the installation seamless as well. However, for a C language package that has only sources published, pip needs a working and properly set up compiler to successfully install the package.
A wheel file is a package distribution. It can contain pure-Python code but also precompiled executable binaries if required. A single package can offer multiple wheels per different Python versions and operating systems. An example wheel file containing precompiled binaries is numpy-1.16.2-cp27-cp27m-win32.whl, for numpy package, available from pypi Download files section for the package. If you are using pip with no problems, you do not need to worry about wheel files.
pip freeze > requirements.txt
pip install -r requirements.txt
- pip - The Python Package Installer, pip.pypa.io
- Installation in pip documentation, pip.pypa.io
- How do I install pip on Windows?, stackoverflow.com
- Installing Packages, packaging.python.org
- Packaging Python Projects, packaging.python.org
- The Python Package Index (PyPI), pypi.org
- Over 10% of Python Packages on PyPI are Distributed Without Any License, 2018, snyk.io