A Comprehensive Guide on Python Package Installation and Management Using pip
Simplifying How to use PIP for Python Package Installation and Management
Table of contents
- Introduction
- What is Package?
- What is Python Package manager?
- What is Python Package Index?
- What is PIP?
- Installed Python Version
- How to install pip
- Check pip installation
- pip not installed?
- pip commands and options
- How to install a package using pip
- How to use Python Package
- How to remove a package or uninstall
- Update Package
- View all installed packages
- View outdated packages
- List the requirement of the current packages
- Conclusion
Introduction
In this article, we'll learn in-depth about python package manager, tools and how to use PIP to install and use python packages in developing applications.
What is Package?
A package is a collection of files that can be used to provide functionalities to codes. It can also be referred to as a bundle of something. For example, a collection of items to be delivered at a location can be referred to as package. It means something is contained within it.
What is Python Package manager?
Python package managers enables installation and management of Python dependencies. They are indispensable tools for developers for installing, managing and updating external libraries when using Python to develop applications. These packages can include reusable codes (functions, modules etc) created by developers to make software development faster and efficient by avoiding to re-invent the wheel (building from scratch). PIP is the most widely used Python package manager. For more information about the relevant project in the space of Python installation and packaging check here
What is Python Package Index?
A platform to find, install or publish python packages. You can use the input field to search for any package. The pip search package_name is no longer supported.
What is PIP?
PIP (pip installs packages) is the package manager for Python. It is included by default from Python 3.4 version. PIP is used to install and manage python packages from the Python package index or other repositories.
Installed Python Version
Check the version of your python installation before proceeding to install pip.
py --version #Windows
python --version #windows depending on the windows version you're using
python3 --version #macos/linux
How to install pip
NB: pip comes installed with Python by default from Python version 3.4
According to Python documentation as at the time of writing this article:
"pip is automatically installed if you are:
working in a virtual environment
using Python downloaded from python.org
using Python that has not been modified by a redistributor to remove ensurepip "
Check pip installation
pip: is a link to pip3 if there's no other version installed on your computer. The name of pip in Python 3 is called pip3. pip3 is used to install and manage Python3.x. packages. If no versions is found the pip will install pip3 packages.
pip --version
pip3 --version
#does the same thing since i have Python 3.10 installed on my system
pip not installed?
If pip is not installed you can use any of this format
- ensurepip
C:> py -m ensurepip --upgrade #windows
python -m ensurepip --upgrade #linux or macos
C:> py get-pip.py #windows
$ python get-pip.py #Linux Macos
From the terminal you can run this command and follow the promptings
There different ways to install pip depending on your operating system. Pretty easy refer to the documentation here
pip commands and options
To view the available commands and options in pip run the command below.
pip help
How to install a package using pip
You can use pip or pip3 depending on the version of Python you're using to install a package using the command below
pip3 install name_of_package #example
pip install pandas #example
How to use Python Package
The methods of a package are accessible in the code file where the package is added using the import keyword. Let's install pandas and use it to present data in a table format.
pip install pandas
import pandas as pd
locations = {
"regions": ["United Kingdom", "Canada", "Germany"],
"short_code": ["UK", "CN", "GER"]
}
df = pd.DataFrame(locations)
print(df)
How to remove a package or uninstall
pip uninstall pandas
pip3 uninstall pandas
Update Package
To update or upgrade any Python package use the command below.
pip install --upgrade pandas
pip3 install --upgrade pandas
View all installed packages
pip3 list
pip list
View outdated packages
The --outdated flag is used to view the list of outdated packages.
pip list --outdated
pip3 list --outdated
List the requirement of the current packages
The freeze command is used to list the requirement of the current packages.
pip3 freeze
#or
pip freeze
That's all for now. Let's go on with practice to gain mastery!!!!
Conclusion
Every Python developer should become proficient with pip for Python package management. In this article, we have examined the fundamentals of pip, learned how to install, update, and remove packages, and demonstrated an example of how to use packages in application development. Developers can easily create scalable and reliable Python applications, optimize their workflows, and take advantage of a large ecosystem of tools and libraries by utilizing pip.
Find this helpful or resourceful? kindly share with others and feel free to use the comment section for questions, answers, and contributions.