Pip — The complete reference guide
A one stop shop for all the pip commands
Note: For non-members, this article is also available at https://dineshkumarkb.com/tech/pip-the-complete-reference-guide/
Introduction:
pip
is the package manager for python.pip can be used to install python libraries from PyPI. In other words, pip
is the npm
of python
. We use pip to install and manage python libraries that are not part of the python standard library.
Motivation:
Any python developer predeminantly uses pip
day in and out.However, the usage is limited to just install and occasionally upgrade.
This article is a consolidated non-exhaustive list of all the pip commands every python developer should know. This could be a reference for any future pip commands.
Commands:
Install — plain install:
The install command is used to install the specified library from the pypi package. I am not giving the output of pip commands here as they are well known.
$pip install [package name]$pip install boto3
Install — per-user:
This may come in handy in the following scenarios
- You are not using a virtual environment
- You are using a shared PC
- You don’t have admin previleges to install your packages at system level
$pip install --user [package name]$pip install --user boto3
pip packages are installed to the system directories by default.In case of linux it is(/usr/local/lib/python3.8/dist-packages). When installed with--user
flag, it is installed for a specific user.
Here is the code snippet to get sitepackages and usersitepackages.
import site
print(site.getsitepackages())
print(site.getusersitepackages()) # usersite packagesOutput:
['/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.8/dist-packages']
'/home/dineshkumarkb/.local/lib/python3.8/site-packages'
Install — with proxy:
If your system is located behind a proxy and you are unable to download from PyPI directly, you could use a proxy provided by your organization.
$pip install --proxy=<proxy-url> [packagename]
Install — upgrade:
This command upgrades the existing pip library.
$pip install --upgrade [package name]
$pip install --upgrade pyinstaller
Install — from a different host:
Install from a different host apart from PyPI package.
$pip install <packagename> --index <internal host url>
Install — from requirements.txt:
Install from requirements.txt
file.
$pip install -r requirements.txt
Install — editable mode:
Install a local package in editable mode. This is useful if you have a local package and you would like to use it. The local package is still under development and any changes to the package will reflect.
$pip install -e <path to your package/repository>
Install —to a target location:
Install package to a specific location
$pip install -t <package name> <path to install>
$pip install -t matplotlib .
Installs matplotlib to the current directory. This could be useful if you are uploading your project to AWS Lambda
.
Install — from a local directory:
Install from a local directory and do not scan PyPI.
$pip install --no-index --find-links=. certifiOutput:
Looking in links: .
Requirement already satisfied: chardet in ./pipenvexec/lib/python3.8/site-packages (3.0.4)
Here, the find links points to current directory as it has the wheel file for certifi
library.The--no-index
flag specifies not to look in the PyPI package.
Install — from VCS:
The libraries can be installed directly from a version control system like git.This is useful for huge organizations who have their projects as dependencies. If the access to the repository is restricted, this can be installed directly from git.
$pip install SomeProject@git+https://git.repo/some_pkg.git@1.3.1
Uninstall
To uninstall a library..
$pip uninstall [package name]
$pip uninstall pyinstaller
Version:
Check pip version
$pip --versionOutput:
pip 21.0.1 from /home/<username>/.local/lib/python3.8/site-packages/pip (python 3.8)
Show:
Get information about an installed package. This includes the version, the location where the package is installed and its dependencies.
$pip show boto3Output:
Name: boto3
Version: 1.17.29
Summary: The AWS SDK for Python
Home-page: https://github.com/boto/boto3
Author: Amazon Web Services
Author-email: None
License: Apache License 2.0
Location: /home/dineshkumarkb/MyGitHub/pipexec/pipenvexec/lib/python3.8/site-packages
Requires: s3transfer, jmespath, botocore
Required-by:
Freeze:
Predominantly used as an input to requirements.txt
file.
$pip freezeOutput:
altgraph==0.17
appdirs==1.4.3
boto3==1.17.29
...
This could be very helpful if you are using a virtualenv and want to copy all your dependencies to requirements.txt
.
$pip freeze > requirements.txt
List:
Lists all the installed packages. If executed inside a virtualenv, it will list all the packages installed in the virtual env.
$pip listOutput:
Package Version
---------------------- --------------------
alembic 1.1.0.dev0
apache-airflow 1.10.12
apispec 1.3.3
appdirs 1.4.3
apturl 0.5.2
argcomplete 1.12.1
arrow 0.17.0
asn1crypto 0.24.0
attrs 19.3.0
...
Download:
Download the python libraries but don’t install them
$pip download [package name]
$pip download requests
$pip download requests <path to download>
The download command downloads the specified library’s wheel files in addition to their dependecies. The above command should give the following output.By default, the files are downloaded to the current directory.
Output:
Collecting requests
Using cached requests-2.25.1-py2.py3-none-any.whl (61 kB)
Saved ./requests-2.25.1-py2.py3-none-any.whl
Collecting chardet<5,>=3.0.2
Downloading chardet-4.0.0-py2.py3-none-any.whl (178 kB)
|████████████████████████████████| 178 kB 1.4 MB/s
Saved ./chardet-4.0.0-py2.py3-none-any.whl
Collecting certifi>=2017.4.17
Downloading certifi-2020.12.5-py2.py3-none-any.whl (147 kB)
|████████████████████████████████| 147 kB 1.9 MB/s
Saved ./certifi-2020.12.5-py2.py3-none-any.whl
Collecting urllib3<1.27,>=1.21.1
Downloading urllib3-1.26.4-py2.py3-none-any.whl (153 kB)
|████████████████████████████████| 153 kB 1.8 MB/s
Saved ./urllib3-1.26.4-py2.py3-none-any.whl
Collecting idna<3,>=2.5
Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
Saved ./idna-2.10-py2.py3-none-any.whl
Successfully downloaded requests chardet certifi urllib3 idna
Search:
pip search searches for python packages. The default location it searches is the PyPI package (https://pypi.org/pypi).
This does not work now.
$pip search requestsOutput:
ERROR: XMLRPC request failed [code: -32500]
RuntimeError: PyPI's XMLRPC API is currently disabled due to unmanageable load and will be deprecated in the near future. See https://status.python.org/ for more information.
However, this could be useful if you have libraries hosted internally in your organization like artifcatory
.
The default URL could be overridden by the -i flag.
$pip search <internalpackagename> --index <internal-artifactory-url>
Check:
Checks compatibility for installed packages
$pip check requestsOutput:
launchpadlib 1.10.13 requires testresources, which is not installed.
pyasn1-modules 0.2.8 has requirement pyasn1<0.5.0,>=0.4.6, but you have pyasn1 0.4.2.
marshmallow-sqlalchemy 0.24.0 has requirement marshmallow>=3.0.0, but you have marshmallow 2.21.0.
Cache:
pip usually stores all the libraries installed in cache.
$pip cache dirOutput:
/home/<username>/.cache/pip
The wheel files are cached inside dir
directory. Whenever a pip install is triggered, it checks the local cache and then connects to PyPI.
The idea behind the pip cache is simple ,when you install a Python package using pip for the first time ,it gets saved on the cache .If you try to download/install the same version of the package on a second time ,pip will just use the local cached copy
Please note that this will not work in a virtual environment as there may not be a need to cache libraries .
Disable cache :
Install package without saving to cache.
$pip install matplotlib --no-cache-dir
Help:
Displays help for pip commands.
$pip helpOutput:
Usage:
pip <command> [options]Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
Verbose:
Run pip in verbose mode for additional information.
$pip install --verbose click
This produces a bunch of output with overwhelming information.
Disable pip version check:
Disables the periodic check to PyPI if a new version of pip is available.
$pip install --disable-pip-version-check [package name]
I would recommend not disabling this as you may miss important updates from PyPI.
I shall keep adding more commands as and when I happen to use them.
Thanks for reading.