Python PIP
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
Example
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
Example
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
Example
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.
Using pip
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.
Syntax:
pip “keywords”
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 install
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:
Syntax:
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
Example
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
Example
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:
Syntax:
pip install –upgrade “package name”
Let’s upgrade our downgraded python-dateutils package.
$ pip install –upgrade python-dateutils
Example
Our python-dateutils package got upgraded successfully to the latest version!
pip list
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.
Example
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.
Syntax:
pip install -r “file name with extension”
Save the package names in a .txt file like this.
pandas
numpy
scipy
pygame
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.
Example
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
Example
To create a txt file from that list, you can do this:
$ pip freeze > packages.txt
Example
A file named packages.txt is created in the current directory, and this file can be used to install packages in other environments.
pip search
Let’s say you want to check if your system has a package installed, how do you do that? By using pip search command.
Syntax:
pip search “package name”
We will check for the python-dateutil package in our system.
$ pip search python-dateutil
Example
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.
Syntax:
pip show “program name”
Let’s check the information of our python-dateutil package.
$ pip show python-dateutil
Example
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.
Syntax:
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.
Example
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!