Complete Guide for Managing Python Modules using Pip
As you go dealing with multiple projects you need to install plenty of Python packages as per your every project needs.
That’s where you need a tool to manage Python packages. Managing Python modules using Pip is my First and Best choice.
In this tutorial, you will learn about to install and to manage Python packages.
What is Pip?
Pip is a package management tool for installing and managing all the Python packages on your system. You can use it with Windows as well as Linux system for managing Python libraries.
Why do You Need a Separate Package Management Tool like Pip?
There are two problems pip deal with for managing packages in Python.
- User-Friendly Commands: It provides many user-friendly commands so that you can easily install, uninstall or manage Python packages.
- Resolving Package Dependencies: For installing new Python package, there can be dependencies among multiple other packages. If you are installing Python package using pip, it installs all other dependent Python packages.
In this article, I will share all the commands you need to manage Python packages on your Linux as well as Windows systems.
How to Install pip on Your System?
The pip is the most preferred installer program for Python. Starting with Python 2.7.9 for Python 2 and Python 3.4 for Python 3, pip comes preinstalled with the Python binary installation.
If you don’t have Python installed on your system, follow the Python installation guide.
If you are using the latest Python versions, you don’t need to install pip explicitly. You can move to the next section.
How to Check if the Pip is already Installed on Your System?
Simply run the following command, if you are not sure if the pip is installed on your system or not.
If the pip is installed on your system, above command will list out all the Python packages installed on your system.
If it is not installed on your system, it will show the result as…
The program 'pip' is currently not installed. You can install it by typing: sudo apt install python-pip
Command for Installing pip on the Linux System.
Run following command in Linux terminal.
sudo apt install python-pip
Command for Installing pip on Windows System:
If you are a window user, open Python interpreter prompt and run a command given below.
python -m pip install -U pip
As we have installed pip on your system, we will see- how to Install other Python modules using the pip tool?
Managing Python Modules Using Pip Tool
If you are a Windows user…
All the pip commands for managing Python packages work on the command prompt.
Open a command prompt to run pip commands.
If you are a Linux user…
Pip tool works on the Linux Terminal.
Open Terminal to run pip commands.
Note: If you don’t have root privilege, run all the commands using “sudo” keyword.
Installing Python Package using pip
pip install <package_name>
For example, if you want to install a package called NumPy.
pip install panda
Note: Panda is one of the best Python Libraries for Data Science. Explore it if you want to master data science.
Here, all the packages are installed from PyPI (Python Package Index).
What is PyPI?
It is a repository from where all the packages mentioned in command are downloaded and installed. For this, you need an internet connection connected to your system.
Installing Specific Python Package Version
By default, it installs the Python package having the latest version. If you want to install a specific version of the Python package, you can do that.
pip install 'SomeProject==1.4'
SomeProject is the project name and 1.4 is its version.
Installing Python Packages Offline
You can also manually download any package and install it. The downloaded package is bundled with a tar zip file.
pip install <local/path/to/package>
pip install ./downloads/SomeProject-1.0.4.tar.gz
This command is useful when you don’t have an internet connection connected to your system.
Installing Python Packages from Any other Source URL
Instead of installing Python packages from the default PyPI source, you can install it from any valid URL.
pip install --index-url http://my.package.repo/simple/SomeProject
Again, it requires an internet connection.
Once you have installed Python libraries, you can import and use them in your program.
Dealing with Multiple Python Versions and Pip
Do you have multiple Python versions installed on your systems like Python 2.x and Python 3.x?
If yes, sometimes it becomes difficult to manage the Python modules on different Python versions.
Remember, you have to install the modules separately for each version to work.
When you run below command
python -m pip install psutil
It installs the psutil module for the default Python version on your system.
If you want to execute the Python module for a specific Python version, find below command.
python-3.7 -m pip install psutil
This will install psutil Python module for your python-3.7.
To install a Python module on your Python 3 version. run below command.
py -m pip install psutil
This is the proper way of dealing with multiple Python versions and PIP.
Command for Upgrading Pip Tool
While running Pip commands you may sometime come across a similar message as below.
You are using pip version 10.0.1, however version 18.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
If you have an older version of pip, it is always recommended to upgrade the version.
Here is the command to upgrade pip version.
python -m pip install --upgrade pip
Show List of all Installed Packages using pip:
Use freeze command. It lists out all the installed Python packages.
You can also use the list command.
It shows out all the installed Python packages on your system with its version.
Also, watch a video where I have given a live demo and shared some of the very useful pip tricks. These tricks will help you managing installed Python packages.
If you want to learn it hard way, check this article about listing Python packages.
Remove / Uninstall Python Package
Use pip uninstall command to uninstall Python module from your system.
pip uninstall <package_name>
For Example, here I’m install panda Python module that I have installed earlier.
pip uninstall panda
Note: You don’t need to provide Python package version while uninstalling.
List of Pip Commands
Pip is the best tool for managing Python packages.
If you want to explore it further, there are many useful pip commands to make your work easy.
You can check all pip commands using
|freeze||Output installed packages in requirements format.|
|list||List installed packages.|
|show||Show information about installed packages.|
|check||Verify installed packages have compatible dependencies.|
|config||Manage local and global configuration.|
|search||Search PyPI for packages.|
|wheel||Build wheels from your requirements.|
|hash||Compute hashes of package archives.|
|completion||A helper command used for command completion.|
|help||Show help for commands.|
Exploring these pip commands and keeping them handy, will really help you with your Python project management activities in the long run.
In this tutorial, you have learned all the basic pip commands and managing Python modules using Pip. If you have any point to discuss, feel free to write a comment.