Pip is a Python package manager tool. If you’ve worked with Python before, you’ve almost certainly used pip as well. Installing packages is the most obvious use of pip, but there are a couple of other useful commands you may not be aware of.
Pip can quickly and efficiently download, install, and manage Python packages.
You’re probably wondering what exactly a Package Manager is and what it accomplishes.
What is a Package Manager?
A package manager is a tool for computer languages that makes it simple to install external dependencies. It installs and uninstalls any package without a problem.
Package management in Python has grown in popularity and importance to the point where it is now included by default with the Python installation starting with versions 3.4 for Python3 and 2.7.9 for Python2.
12 pip Commands For Python Developers
1)Pip Install and Uninstall Package
This command is essential for installing and uninstalling a package according to your needs. You can use this command on your command prompt to install or uninstall Python by heading to the Python folder.
pip install packageName
pip uninstall packageName
If you want to install & uninstall matplotlib module then follow the below commands:
pip install matplotlib - for installation
pip uninstall matplotlib - for Uninstallating
If the pip is not in its latest version, you can also upgrade it. To check if it has an updated version, simply type pip –version; if it does not, simply update it using the command below. Make sure this command is written in the Python folder.
pip install –upgrade pip
Here the command pip install –upgrade pip, updates pip to the most recent/latest version.
pip install -U <packageName>
If we want to update the version of numpy, below is the command:
pip install -U numpy
4)Update All Packages
If an older version of the package exists, pip will remove it first. It will then download and install the most recent version from PyPI. If you already have an up-to-date version installed, pip will do nothing.
To update all packages at once, use the command pip freeze > requirements.txt to create a requirements.txt file that contains all of the packages. To update all of the packages, simply update the requirements.txt file.
Packages with updated versions will be excluded in the requirements.txt file, while the remainder will be updated.
pip freeze > requirements.txt
pip install -r requirements.txt –upgrade
pip freeze requirements.txt generates a temporary file named requirements.txt that may include all of the packages that need to be installed, and then pip install -r requirements.txt –upgrade updates all of the items in requirements.txt at once.
5)Information about an Installed Package
You can get the information about the package that is installed already using the following command:
pip show packageName
You get information about the numpy package that is installed already by:
pip show numpy
pip will return a “Package Not Found” warning if you try to acquire information about a package that isn’t installed in your environment.
5)List all the installed Packages
This is one of the most essential commands for any Python programmer. The pip list command displays the list of packages in the current environment. For each package, it also returns the installed version. The packages are arranged in a case-insensitive manner.
All packages are listed using pip list. However, we may want to display all of the list of packages that are currently outdated for any reason.
To do so, run the command:
"pip list -o" or
"pip list —outdated,"
which will provide a list of packages with both the currently installed version and the most recent accessible version.
When you’re working on a project and need a package but don’t know which location we can fing it , then use the pip search packageName command. It returns the name of all matching packages as well as information about them.
pip search packageName
pip search pandas
This command helps you navigate to pandas by searching the path where it is kept with pip search pandas.
7) Generating Requirement Files using pip
What is requirements.txt file in a code?
Generally, We create(generate) and share the requirements.txt files to make it easier for other developers to install the righ/correct versions of the required Python libraries (or “packages”) in order to run the Python code that we developed.
We can just execute the below command to generate the requirements.txt file:
pip freeze > requirements.txt
This code will create a requirements.txt file in the working directory after execution.
We can now simply use the following command to install all dependencies on their system.
pip install -r requirements.txt
After you’ve installed these dependencies, you’re ready to go.
8)Activate Editable Mode
If you want to develop a package locally after installing it, you can do so in editable mode. It connects the package to the given location, allowing you to make modifications that are immediately reflected.
The below command installs the package in develop or editable mode, which allows you to alter it.
pip install -e
That means, pip install -e opens the package(whichever was previously installed) in an editable form, allowing you to make modifications locally if necessary.
9)Verifying If the installed Packages have compatible dependencies
It checks if the dependencies of all installed packages in the requirements.txt file are appropriate. It examines all three requirements, namely whether the dependencies are compatible, whether the package has an updated version, and whether a package is missing.
10)To Remove All the Installed Packages by pip
Instead of deleting packages one by one, you can save time by using the commands
pip freeze > requirements.txt && pip uninstall -e requirements.txt -y.
It creates a requirements.txt file with a list of packages and then uninstalls them all.
11)Using pip to specify the Package Version
In Python, the pip install command always installs the most latest versions of a package. To install a specific version of the package rather than the most recent, we can use the pip command to specify the version directly.
pip install packageName==version
pip install pandas==2.7
The command above will download and install the package ‘package’ of the version we specified i.e 2.7 (and not the latest one). It will be installed in your Python installation folder, or if you are using a virtual environment, it will be installed here.
12)To install all the requirements from requirements.txt File
pip install -r requirements.txt
The above command will install all of the dependencies required for project implementation. Dependencies contain all of the package’s information.