1. Introduction
In python development, the utilization of various packages is a common practice. Pip plays a vital role in managing these packages. In this article, we will be covering basic package management using pip including installation, uninstallation, and upgrading of packages.
What is a pip?
Python has a rich ecosystem of libraries and packages. Managing these packages is crucial for any python developer, and that's where pip comes into play. Pip is a standard package manager for python. It allows you to install packages from the Python Package Index and other indexes.
History and evolution of pip
First introduced as pyinstall in 2008 by Ian Bicking (the creator of the virtualenv package) as an alternative to easy install. Pip was chosen as the new name from one of several suggestions that the creator received in his blog post. According to Bicking himself, the name is a recursive acronym for "Pip Installs Packages". In 2011, the Python Packaging Authority (PyPA) was created to take over the maintenance of pip and virtualenv from Ian Bicking, led by Carl Meyer, Brian Rosner, and Jannis Leidel.
Over the years, it has evolved into a standard package manager for python, offering enhanced features and performance. Major features include dependency resolution, version management, and integration with virtual environments.
2. Installing pip
Verify python installation
Before installing pip verify that python is installed in your system by running the following command in the terminal/command prompt.
Windows
python --version
Linux/macOS
python3 --version
after running the command if it returns something like Python 3.12.1
then it means python is already installed in your system otherwise you can download it here Download Python.
Installation of pip
Once python is installed you can proceed further to install pip by running the following commands in the terminal/command prompt.
Windows
Run the following command to download the get-pip.py script.
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Now execute the downloaded python script by running the following command.
python get-pip.py
Finally, verify the installation by running the following command.
pip --version
Linux
Run the following command to download and install pip.
sudo apt-get update
sudo apt-get install python3-pip
You can verify the installation by running the following command.
pip3 --version
macOS
Run the following command to download the get-pip.py script.
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Now execute the downloaded python script by running the following command.
python get-pip.py
Finally, verify the installation by running the following command.
pip3 --version
3. Basic usage of pip
Syntax for installing packages using pip
To install a package using pip you have to follow the following syntax.
pip install package_name
You can go more specifically when installing python3 packages to avoid potential conflicts as follows.
pip3 install package_name
Installing packages using pip
Let's try to install a package named pandas using pip.
pip3 install pandas
If you have followed the tutorial step by step, the pip will start installing pandas after running the above command.
You can also install external packages and use them in your code other than available in Python Package Index.
4. Installing specific package versions
Specifying package versions
If you are a developer you might be working on existing projects that rely on specific versions of packages. In such cases, you have to install packages that are compatible with your project for that you have to specify the package version while installing it.
pip3 install package_name==version
Let's say you want to install pandas version 1.3.4 here's how you will do it.
pip3 install pandas=1.3.4
Specifying package version ranges
Similarly, you can specify the minimum and maximum version ranges.
pip3 install package_name>=min_version
It will install the package of version equal to or greater than min_version
.
pip3 install package_name<=max_version
It will install the package of version equal to or less than max_version
.
You can use the above syntax to install packages in the specified range.
pip3 install pandas>=1.3.4
The above command will install a pandas version equal to or greater than 1.3.4
.
5. Upgrading and uninstalling packages
Often while working with python you have to upgrade packages or remove the packages from your project so let's see how you can do that using pip.
Upgrading packages
To upgrade an existing package using pip you can follow the below syntax.
pip3 install --upgrade package_name
or
pip3 install -U package_name
You can upgrade your packages by following either of the syntax. Let's upgrade the pandas package to the latest version.
pip3 install --upgrade pandas
or
pip3 install -U pandas
Both commands will upgrade the pandas to the latest version you can use either of them.
The command 'pip3 install -U pandas' will also install the pandas if it does not exist in your project
Upgrading pip
You can upgrade the pip using pip itself, yea it looks odd but trust me it works considering the syntax below.
pip3 install --upgrade pip
or
pip3 install -U pip
As we discussed before, you can follow either syntax to upgrade the pip.
Uninstalling packages
You can uninstall a package by using the following syntax.
pip3 uninstall package_name
Let's try to uninstall pandas using the above syntax.
pip3 uninstall pandas
You can upgrade and uninstall multiple packages at the same time as : 'pip install --upgrade package_name_1 package_name_2...' and 'pip uninstall package_name_1 package_name_2...'
6. Conclusion
In this article, we discussed Pip as the standard package manager in Python, emphasizing its versatility in package installation, upgrading, and removal. Through commands like pip3 install package_name
for installation and pip3 install --upgrade package_name
for upgrades, Pip facilitates efficient package management. Furthermore, we explored installing specific package versions and uninstalling packages using pip3 uninstall package_name.
Looking ahead, in the next article we will dive into advanced Pip usage.
7. References