Why PIP is required
Python is regarded as a battery-included language. This means that the Python standard library comes with many packages and modules to aid programmers with developing their code.
Simultaneously, Python has a vibrant community that contributes an even larger number of packages to aid development. The Python Package Index, or PyPI, is where these packages are released. PyPI contains many packages, such as development frameworks, tools, and libraries.
Many of these packages make Python development easier by offering user-friendly interfaces to features found in the standard library.
How to install packages using PIP
We can use the help command to list all the supported commands for getting started with pip.
pip help
After executing the above command in the console, you will get a list of pip-supported commands, one of which will be installed. You can use this install command to install packages.
You need to use the pip install command followed by the package name for installing packages.
pip install package_name
PIP also install the required dependencies, if any, required before installing the package,
Suppose you want to install the library pandas.
You need to execute the below command.
pip install pandas
>> Collecting pandas
Downloading pandas-1.3.5-cp39-cp39-win_amd64.whl (10.2 MB)
|████████████████████████████████| 10.2 MB 3.2 MB/s
Collecting pytz>=2017.3
Downloading pytz-2021.3-py2.py3-none-any.whl (503 kB)
|████████████████████████████████| 503 kB 6.4 MB/s
Requirement already satisfied: numpy>=1.17.3 in c:\users\apurv\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from pandas) (1.20.2)
Collecting python-dateutil>=2.7.3
Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
|████████████████████████████████| 247 kB 6.8 MB/s
Requirement already satisfied: six>=1.5 in c:\users\apurv\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from python-dateutil>=2.7.3->pandas) (1.16.0)
Installing collected packages: pytz, python-dateutil, pandas
Successfully installed pandas-1.3.5 python-dateutil-2.8.2 pytz-2021.3
You should see output similar to above. The package is successfully installed.
If the package version is not specified, the latest version is installed. If you want to install a specific version, use the following command.
pip install package_name==version
You can also read about the Multilevel Inheritance in Python, Swapcase in Python
More useful PIP commands
How to upgrade pip version
If you see the following text at the bottom of the output after using some pip command, then this means that you have an older version of pip installed.
WARNING: You are using pip version 21.2.4; however, version 21.3.1 is available.
You should consider upgrading via the 'C:\Users\apurv\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe -m pip install --upgrade pip' command.
To update and get the latest version of pip, use the following command.
python -m pip install --upgrade pip
Get a list of installed packages
The below command can get a list of all the installed packages.
pip list
>>
Package Version
--------------------- ------------
absl-py 0.12.0
attrs 21.2.0
beautifulsoup4 4.9.3
certifi 2021.5.30
chardet 4.0.0
click 7.1.2
colorama 0.4.4
Get more info about a package
The pip show command followed by the package name fetches more information or metadata about some specified package. For example, if we want to get metadata of package NumPy, we can use
pip show numpy
>>
Name: numpy
Version: 1.20.2
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email:
License: BSD
Location: c:\users\apurv\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages
Requires:
Required-by: imageio, mediapipe, numba, opencv-contrib-python, opencv-python, pandas, scipy
Also see, Convert String to List Python
Requirements File
Sometimes while developing projects, there may be cases when the project is developed using some specific version of a library, and it works with that version only.
This might create problems if a package is installed using the pip install command without specifying the version. The latest version is downloaded in such a case.
So to take note of the packages and their versions in the requirements file, the pip freeze command is used. Executing the pip freeze command stores the output in the requirements file format.
pip freeze > requirements.txt
Now, this requirements.txt file can be used to get the exact environment in any other system for the proper execution of the software.
To install the required packages from the requirements.txt file, use the following command.
pip install -r requirements.txt
Thus, all the packages will be installed with specified versions.
Uninstalling packages
You'll have to uninstall a package now and then. Either you've found a better library to replace it, or it's something you don't require. It can be a little tricky to uninstall a package using pip.
Every package has some dependencies. When installing a package, all its dependencies are automatically installed. Hence some more packages may have been automatically installed when installing a package.
pip show scipy
Name: scipy
Version: 1.7.1
Summary: SciPy: Scientific Library for Python
Home-page: https://www.scipy.org
Author:
Author-email:
License: BSD
Location: c:\users\apurv\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages
Requires: numpy
Required-by:
We can see two fields from the above output, Requires, and Required-by. It tells us that scipy requires NumPy and no other package requires it. Hence we can safely uninstall the package by the following command.
pip uninstall scipy
You can also check the Requires packages and if safe to uninstall, uninstall them too as they may be redundant after uninstallation of the main package.
Also see, How to Check Python Version in CMD
FAQs
-
Why is pip required?
Even though Python’s standard package is vast, Python developers are constantly developing new packages, which further aids in development. These packages are released in PyPI, which contains many packages, such as development frameworks, tools, and libraries.
-
Who developed pip?
It was developed by Ian Bicking in 2008 and was initially named pyinstall. It was introduced as an alternative to easy_install.
-
How to get a list of all commands supported by pip?
You can use the pip help command to get all the commands supported by pip.
Key Takeaways
Congratulations on making it this far.
This blog introduced us to the basics of a third-party installer (pip) in Python. We understood what it is and why it is necessary even with the presence of a huge standard library of Python. We learned how to install, uninstall and modify packages and different pip commands.
Check out this article - String slicing in Python
If you want to take your learnings to next level, you can visit and read our library of curated blogs by clicking here.