If you are new to the Python programming language or have some experience in navigating around popular programming languages, then you must have crossed paths with PIP.
The Python module installed on your Linux operating system distribution is associated with numerous packages and libraries that help lessen common hurdles affecting your Python projects.
To install such packages and libraries, we need the aid of Python PIP, which is a useful Python package manager that is effective in fetching, installing, and configuring needed Python packages and libraries.
The usage of the PIP Python package manager is not always clear and might require continuous internet searches to find the appropriate command syntax associated with it.
[ You might also like: How to Install Python PIP in Linux ]
This article guide will unravel some useful Python PIP cheats that should help us master the usage of this Python-based utility.
The reference syntax for executing commands through Python PIP is as follows:
$ python3 -m pip <command> [options]
Install Python Virtual Environment
In the above case, the Python PIP command is globally executed. However, when you have multiple Python projects and need to run them concurrently, it is advisable to make use of a virtual environment so that your project modules do not mix.
The syntax for the above command under a virtual environment will look as follows:
(env_name)$ python3 -m pip <command> [options]
To install a virtual environment, run:
$ python3 -m pip install --user virtualenv
Create Python Virtual Environment
To create a Python virtual environment, run:
$ python3 -m venv my_venv
You will need to install python3-venv from your distribution package manager before executing the above command:
$ sudo apt install -y python3-venv [On Debian, Ubuntu and Mint] $ sudo yum install python3-venv [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a dev-python/virtualenv [On Gentoo Linux] $ sudo apk add python3-venv [On Alpine Linux] $ sudo pacman -S python3-venv [On Arch Linux] $ sudo zypper install python3-venv [On OpenSUSE]
To activate the Python virtual environment, run:
$ source my_venv/bin/activate
To deactivate the Python virtual environment, run:
(my_venv) $ deactivate
You can create as many virtual environments as you like for each of your Python projects.
Python PIP Commands Cheat Sheet
In this section, we will look at some of the useful pip commands usage with examples.
PIP – Install Python Package
To install a Python package/library, we will run:
$ python3 -m pip install pillow
PIP – Download Python Package
To download a Python package/library instead of directly installing it, run:
$ python3 -m pip download flask
PIP – Uninstall Python Package
To uninstall a Python package/library, run:
$ python3 -m pip uninstall requests
PIP – Copy Installed Packages
Supposing that you used a Python virtual environment to install the packages and libraries you needed to run your project. To transfer and run the project on a different Linux operating system environment/platform, you will need a list of these packages/libraries.
This list can be created in the following manner:
$ python3 -m pip freeze > requirements.txt $ cat requirements.txt
PIP – Install Package from File
To install the packages/libraries on the requirements list, we will run:
$ python3 -m pip install -r requirements.txt
PIP – View Virtual Environment
To inspect an existing virtual environment, run:
$ python3 -m pip name_of_virtual_environment
If the virtual environment is correctly set up, you should get an output similar to the following:
PIP – List Installed Packages
To list all PIP-installed packages/libraries, run:
$ python3 -m pip list
PIP – Check Package Information
For detailed information on a specific installed package/library, run:
$ python3 -m pip show pillow
PIP – Check Package Dependencies
If you wish to make sure an installed package/library has no broken dependencies, run:
$ python3 -m pip check pillow
PIP – Search for Package
The following PIP approach for searching for Python packages prior to their installation is permanently banned and therefore you will need to directly search Python packages at pypi.org before installing with PIP:
$ python3 -m pip search flask
PIP – Debugging for Errors
To perform some debugging in case you encounter some errors, run:
$ python3 -m pip debug
PIP – Get Command Help
To get help in using a specific PIP command, run:
$ python3 -m pip help config
PIP – List of Commands
For a complete list of PIP commands, run:
$ python3 -m pip --help
With this PIP cheat sheet article, you should be able to explore your Python projects to their full potential.