What is pip, Getting Started with Python pip

Read it in 9 Mins

Last updated on
01st Dec, 2022
Published
30th Oct, 2019
Views
21,238
What is pip, Getting Started with Python pip

Pip is a package manager for Python that allows you to install additional libraries and packages that are not part of the standard Python library such as the ones found in the Python Package Index. It is a replacement for an easy install. If your version of Python is 2.7.9 (or greater) or Python 3.4 (or greater), then PIP comes pre-installed with Python, in other cases, you will have to install it separately. Here is an article about sys.argv command line argument in Python for better understanding. 

Read about Self in Python here!

PIP is a recursive acronym for “Preferred Installer Program” or PIP Installs Packages. It is a command-line utility that installs, reinstalls, or uninstalls PyPI packages with one simple command: pip. You may be familiar with the term package manager if you have used other languages like Ruby uses Gem, JavaScript uses npm for package management, and .NET uses NuGet. Pip has become the standard package manager for Python. 

The Python installer installs pip automatically, so it is ready for you to use unless you have installed an older version of Python. You can also verify if pip is available on your Python version by running the command below:

PIP in Python

On running the command mentioned above, a similar output should be displayed which will show the pip version, along with the location and version of Python. If you are using an older version of Python, the pip version will not be displayed. Then you can install it separately. 

You can download pip from the following link:
https://pypi.org/project/pip/ 

Installing pip in Python

For Windows

Follow the instructions to install pip in Python on Windows 7, Windows 8.1, and Windows 10: 

  • Download get-pip.py installer script from https://bootstrap.pypa.io/get-pip.py. For Python 3.2, download from after that, right-click on the link and select Save As and save it to any safe location on your computer. 
  • Open Command Prompt and navigate to the get-pip.py file where you saved it previously. 
  • Run the command: python get-pip.py 

In case you are a beginner and struggling to make inroads, we recommend taking a python programming course to shorten your learning curve.  

For Mac 

Modern Mac systems have Python and pip pre-installed but the version of Python tends to be outdated and not the best choice for serious programming in Python. So, it’s highly recommended that you install a more updated version of Python and PIP. 

If you want to use the pre-installed Python application but don’t have PIP available, you can install PIP with the following commands in Terminal:

sudo easy_install pip

If you want to install an updated version of Python, then you can use Homebrew. Installing Python with Homebrew requires a single command:

brew install python

Installing Python with Homebrew will give you the latest version which should come packaged with PIP but if PIP is unavailable, you can re-link Python using the following commands in Terminal:

brew unlink python && brew link python

For Linux 

If your Linux distribution came with Python pre-installed, using your system’s package manager you will be able to install PIP. This is preferable since pre-installed versions of Python do not work well with the get-pip.py script used on Windows and Mac. Given below are the commands you should run in order to install pip in your system depending on the version of Python you are using:

Advanced Package Tool (Python 2.x):

sudo apt-get install python-pip 

pacman Package Manager (Python 2.x):

sudo pacman -S python2-pip 

Yum Package Manager (Python 2.x):

sudo yum upgrade python-setuptools 
sudo yum install python-pip python-wheel 

Dandified Yum (Python 2.x):

sudo dnf upgrade python-setuptools 
sudo dnf install python-pip python-wheel 

Zypper Package Manager (Python 2.x):

sudo zypper install python-pip python-setuptools python-wheel 

Advanced Package Tool (Python 3.x): 

sudo apt-get install python3-pip 

pacman Package Manager (Python 3.x): 

sudo pacman -S python-pip 

Yum Package Manager (Python 3.x): 

sudo yum install python3 python3-wheel 

Dandified Yum (Python 3.x): 

sudo dnf install python3 python3-wheel 

Zypper Package Manager (Python 3.x): 

sudo zypper install python3-pip python3-setuptools python3-wheel 

For Raspberry Pi 

You are most likely running Raspbian if you are a Raspberry Pi user as it is the official operating system designated and provided by the Raspberry Pi Foundation. PIP comes pre-installed on with Raspbian Jessie. It is one of the biggest reasons to upgrade to Raspbian Jessie instead of using Raspbian Wheezy or Raspbian Jessie Lite. If you are using an older version of Raspbian, you can still manually install PIP. Given below are the commands you should run in order to install pip on your system depending on the version of Python you are using: 

On Python 2.x:

sudo apt-get install python-pip

On Python 3.x:

sudo apt-get install python3-pip

Raspbian users, working with Python 2.x must use pip while Python 3.x users must use pip3 while running PIP commands.

For Ubuntu

sudo apt-get install python-pip

For Fedora

sudo yum install python-pip

How to use PIP and PyPI? 

PyPI - the Python Package Index 

After PIP is installed, we need to find a package to install. Packages are usually installed from the repository of software for the Python programming language which is the Python Package Index.

Set environment variable for PIP:

You won’t have to reference the pip install directory again and again if you set an environment variable.

Set: (default = C:\Python27\Scripts) in your Windows/Linux “PATH” environment variable.

Getting Started with PIP 

Now that we know what PIP is and have successfully installed it on our computer, let's get started on how to use it: 

Commands in PIP 

Enter pip in the command terminal and it will show the following output on the screen. 

Usage:
pip [options] 

Commands:

InstallInstall packages
DownloadDownload packages
uninstallUninstall packages
unzipUnzip individual packages
bundleCreate pybundles
helpShow help for commands
configManage local and global configuration
freeze
Output installed packages in required format
listList installed packages
wheelBuild wheels from your requirements
hashCompute hashes of package archives
completionA helper command used for command completion
checkVerify installed packages have compatible dependencies
showShow information about installed packages
searchSearch PyPI for packages
zipZip individual packages

Commonly used commands in pip are install, upgrade or uninstall. 

General Options: 

-h, --help: Shows help. 

--isolated: To run pip in an isolated mode by ignoring environment variables and user configuration. 

-v, --verbose: Give more output. Option is additive, and can be used up to 3 times. 

-V, --version: Show version and exit. 

-q, --quiet: Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels). 

--proxy: Specify a proxy in the form [user:passwd@]proxy.server:port. 

--trusted-host: Mark this host as trusted, even though it does not have valid or any HTTPS. 

--certPath to alternate CA bundle. 

--client-cert: Path to SSL client certificate, a single file containing the private key and the certificate in PEM format. 

--retries: Maximum number of retries each connection should attempt(5 times by default). 

--timeout: Set the socket timeout(15 seconds by default). 

--exists-action: Default action when a path already exists: (s)witch,(i)gnore, (w)ipe, (b)ackup, (a)bort). 

--cache-dir: Store the cache data in <dir>.

--no-cache-dir: Disable the cache. 

--disable-pip-version-check: Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index. 

Finding required packages:

To search any package, i.e. Flask command will be as shown below: pip search Flask 

The following output will be displayed with all packages and description: 

Flask-Cache - Adds cache support to your Flask application
Flask-SeaSurf - An update CSRF extension for Flask
Flask-Admin - Simple and extensible admin interface framework for Flask
Flask-Security - Simple security for Flask apps
Flask - A microframework based on Werkzeug, Jinja2 and good intentions 

Installing a package: 

To install the required package, in our case it is Flask, enter the following command : 

pip install Flask 

Pip – Show information 

To check information about the newly installed packages enter: 

pip show Flask 
--- 
Name: Flask 
Version: 0.10.1 
Location: /usr/local/lib/python2.7/dist-packages 
Requires: Werkzeug, Jinja2, itsdangerous

Uninstalling a package:

To uninstall any package installed by PIP, enter the command given below. 

pip uninstall Flask
Uninstalling Flask:
...
.....
Proceed (y/n)? 
Successfully uninstalled Flask 

That’s all. The PIP application has been uninstalled. 

How to Upgrade PIP for Python 

Although PIP application doesn’t receive updates very often, it’s still important to keep the application up to date with the newer versions because there may be important fixes to bugs, compatibility, and security holes. Fortunately, upgrading to the latest versions of PIP is very fast and simple yet quite a few learners often seek help from our experts leading a python programming advanced course on KnowledgeHut.

On Windows 

python -m pip install -U pip 

On Mac, Linux, or Raspberry Pi 

pip install -U pip 

Certain versions of Linux and Raspberry Pi, pip3 needs to be entered instead of pip. 

Using Requirement Files

The pip install command always installs the latest published version of a package, but you should install the particular version that suits your code. 

You would want to create a specification of the dependencies and versions that you have used while developing and running your application, so that there are no surprises when you use the application in production. 

Requirement files allow you to specify exactly the packages and versions that should be installed on your system. Executing pip help shows that there is a freeze command that displays the installed packages in requirements format. This command can be used to redirect the output to a file to generate a requirements file: 

pip install Requirements in Python

The freeze command is used to dump all the packages and their versions to standard output, so as to redirect the output to a file that can be used to install the exact requirements into another system. The general convention is to name this file requirements.txt, but it is completely up to you to name it whatever you want. 

If you want to replicate the environment in another system, run pip install specifying the requirements file using the -r switch:

The versions listed in requirements.txt will match those of the packages: 

$ pip list 

Package    Version 
----------        ---------- 
certifi2018.11.29 
chardet3.0.4 
idna   2.8 
pip    19.0.1 
requests     2.21.0 
setuptools  40.6.2 
urllib31.24.1 

You may submit the requirements.txt file to source control and can use it to create the exact environment in other machines. 

Fine-Tuning Requirements 

The problem with hardcoding the versions of your packages and their dependencies is that the packages receive frequent updates with bug and security fixes, and you probably want to update to them as soon as they are published. 

The requirements file format gives you a bit of flexibility to ensure that the packages are up to date by allowing you to enter dependency versions using logical operators, although specifying the base versions of a package. 

Make the following changes by opening the requirements.txt file in your editor: 

certifi>=2018.11.29 
chardet>=3.0.4 
idna>=2.8 
requests>=2.21.0 
urllib3>=1.24.1 

Change the logical operator to >= to install an exact or greater version that has been published. While you set a new environment using the requirments.txt file, pip searches for the latest version that supports the requirement and installs it. The packages in your requirements file can be updated by running the install command with the --upgrade switch: 

Fine-Tuning Requirements in Python

In this case nothing was upgraded because latest versions have already been installed, but if a new version was published for a listed package, then the package would’ve been upgraded. 

New versions can introduce changes that fix bugs and will make or break your application. In order to fine-tune your requirements, the requirements file syntax supports additional version specifiers. 

Let us assume that a new version 3.0 of requests is published but it breaks your application as it introduces an incompatible change. In such a case, the requirements file can be modified to prevent 3.0 or higher versions from being installed: 

certifi>=2018.11.29
chardet>=3.0.4
idna>=2.8
requests>=2.21.0, <3.0
urllib3>=1.24.1

Changing the version specifier for the requests package ensures that only the versions which are less than 3.0 get installed. 

Production vs Development Dependencies

All packages which are installed during the development of your applications are not going to be application dependencies. During the development process, there are certain packages published to PyPI that are development tools or libraries that can be useful to you. 

For example, you would require a unit test framework in order to unit test your application. Pytest is a popular framework for unit testing. You would want to install the unit testing framework in your development environment, but not in your production environment because it is not an application dependency. 

To set up a development environment, you need to create a second requirements file (requirements_file.txt) to list additional tools: 

# In requirements_file.txt 
pytest>=4.2.0 

To do this, you need to install both requirement files using pip: requirements.txt and requirements_file.txt. Pip allows for specifying additional parameters within a single requirements file. The requirements_file.txt can also be modified to install the requirements from the production requirements.txt file: 

# In requirements_file.txt 
-r requirements.txt 
pytest>=4.2.0 

Notice that the exact same -r switch is being used in order to install the production requirements.txt file. The file format of the requirements file allows you to specify additional arguments right on a requirements file. 

Alternatives to pip

Pip is an essential tool for all Pythonistas which is used in developing many applications and projects for package management. This article gives you the basics of Pip for Python but the Python community is very active in providing great tools and libraries for developers using other applications as well. These include alternatives to pip that try to improve and simplify package management. 

Here are some package management tools other than pip that are available for Python: 

  • Conda
  • Poetry
  • Pipenv

Summary

By now you know that pip is a package manager for Python that is used in many projects to manage dependencies. It is included with the Python installer, hence it is essential for all Python programmers to know how to use it. 

Although Python provides a wide range of standard libraries which are suitable for developing all types of applications, the active Python community provides more sets of tools and libraries that speed up the development process of a Python application. 

In this article, we have covered: 

  • The process of installing pip in Python 
  • Setting an environment variable for pip 
  • Commonly used commands in pip and their functions 
  • Finding and installing new packages using pip with requirement files in the command line and getting information about the newly installed package 
  • How to uninstall a package in pip? 

In addition to the above topics we have also covered the importance of keeping dependencies updated and a few alternatives to pip that can help managing those dependencies. To gain more knowledge about Python tips and tricks, check our Python tutorial and get a good hold over coding Python by joining the Knowledgehut python programming course.

Profile

Priyankur Sarkar

Data Science Enthusiast

Priyankur Sarkar loves to play with data and get insightful results out of it, then turn those data insights and results in business growth. He is an electronics engineer with a versatile experience as an individual contributor and leading teams, and has actively worked towards building Machine Learning capabilities for organizations.

Develop the skills of the future via outcome-based immersive learning

Speak to our Career Advisor now!

Select
Your Message (Optional)