You might have heard the name before but what exactly is pip in the Python language? There are many third-party packages that you can install on your system. These packages make Python to have some of the best libraries in the whole programming world.
To install and maintain these packages, a programming language needs a package manager. pip is the standard package manager for Python. pip is so important in Python that it now comes pre-bundled with Python versions 3.4 and above.
You can check the version of Python installed on your system using one of the following commands depending on your operating system.
How to check Python version?
Open a command prompt/powershell if you are on Windows or a terminal if you are either on macOS or Linux. Then, enter the following command.
$ python –version
You should see your Python version now. There can be a possibility that a third-party software like Anaconda has already installed pip in your system without your knowledge. You can check installation of pip using this command.
$ pip help
If you get an output that confirms that pip responded, then great, Python pip is installed on your Windows/Linux/Mac machine. However, if your system cannot recognize the command “pip”, then you will need to install it.
Follow the steps in this tutorial to install pip.
How to install PIP?
Open a command window or a terminal depending on your OS, and then enter these two commands.
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python get-pip.py
The first line downloads pip for Python and the second one installs it. Now your system has Python pip installed, if it was not the case before!
How to Update PIP version?
Python requires pip to be at least of a specific version to be compatible with it. If you are using an old version of pip, it is recommended to install the latest pip version.
It is done using this command:
python -m pip install –upgrade pip
If you choose to do this, you might not have a cached file to install the latest version, so your system will download the latest version.
Now that you have installed pip, it makes you able to use the pip command in the command line software to do various things with pip.
pip can accept various keywords like, install, list, etc. to perform different tasks for the user. First, we will see how to install a package using pip
pip uses the install command to fetch an installation request. Python is an open-source programming language and its community is so vast that you can get many packages to perform a specific task.
You can install a package in the command line like this:
pip install “package name”
Let’s install a package named python-dateutil that is one of the most installed packages in Python. It is used to provide a powerful extension to the Python datetime package.
$ pip install python-dateutil
As you can see, we get a message that the package has been successfully installed. This means that you can now go to your IDE and import this package.
How to install a specific version of pip package?
When developers update their packages, they might have some bugs or a particular change that does not suit your code. So, it can be better not to install the latest version of that package and install the older version.
You can specify the version of the package that you want to install like this.
$ pip install python-dateutil == 2.7.0
First Python checks if the package is already installed or not, so it outputs a message that “the requirement is already satisfied” but on checking the version that we want to install, it removes the current package version and installs the specified version.
Upgrade pip package
Although sometimes you need to downgrade your package, most of the time it is better to have the latest version of the package. In Python, pip can be used to update the package to the latest version without specifying the latest version. It is done like this:
pip install –upgrade “package name”
Let’s upgrade our downgraded python-dateutils package.
$ pip install –upgrade python-dateutils
Our python-dateutils package got upgraded successfully to the latest version!
You can check all of your installed Python packages using the pip list command in the current active Python environment.
$ pip list
Using this command, it becomes easier to check the installed packages and their versions.
Installing a list of packages
A file that contains the name of all the packages that you wish to install can be used to install all of the packages together using the following syntax.
pip install -r “file name with extension”
Save the package names in a .txt file like this.
You can name it whatever you want and store the file where you can easily access it. Now navigate to the folder where you saved this file, and open a command line software in the folder and enter this command
$ pip install -r “install packages.txt”
All of the packages will get installed. It is an easy way when you do not want to install packages one by one but just want to sit back and relax while the packages get installed while you watch that beautiful command line doing the job.
Share pip packages between Environments
If you have multiple environments to work on different projects, and you want to install the packages installed in an environment on another environment. One easy way to do this is to create a txt file using the pip freeze command.
pip freeze returns a list of all of the third-party packages that you have installed on your system. For example:
$ pip freeze
To create a txt file from that list, you can do this:
$ pip freeze > packages.txt
A file named packages.txt is created in the current directory, and this file can be used to install packages in other environments.
Let’s say you want to check if your system has a package installed, how do you do that? By using pip search command.
pip search “package name”
We will check for the python-dateutil package in our system.
$ pip search python-dateutil
The above command will provide you the installed package and other packages on which this package depends to work. These packages are called “dependencies”.
pip package Information
You can have all of the information about a package like the author’s name, their email, etc., about a package you have installed in your system using the pip show command.
pip show “program name”
Let’s check the information of our python-dateutil package.
$ pip show python-dateutil
It also gives you a summary of what is the functionality of the package.
Removing a pip package
Using the pip uninstall command, an installed package can be removed.
pip uninstall “package name”
The python-dateutil package is not of any use for now, so let’s remove it from the system.
$ pip uninstall python-dateutil
The command line software would ask for your permission to remove the package, simply type “y” for yes and “n” for no to choose the permission.
Packages saves lot of time. So when you are stuck at a complex problem, try searching on the internet for the pip package. pip might have a package for your problem that could make your task easier!