In this article, we will explore pip, Python's package installer. You will learn how to install, update, list, and uninstall packages, as well as understand the basics of the Python Package Index (PyPI) and how to manage dependencies.
Introduction to the Python Package Index (PyPI)
The Python Package Index (PyPI) is the largest repository of Python packages. It contains thousands of community-developed packages that can be used in your projects. Although Python has a rich standard library, you often need additional packages that are not included in it.
You can access PyPI at https://pypi.org/ to explore packages. For example, to find packages related to HTTP requests, simply search for the term requests.
Package Versioning Structure
PyPI packages follow the semantic versioning convention in the format:
major.minor.patch
-
major: Significant changes that are not backward compatible.
-
minor: New features that are backward compatible.
-
patch: Bug fixes and minor improvements.
Example: The requests package in version 2.24.0 has:
-
Major version: 2
-
Minor version: 24
-
Patch version: 0
If you use requests==2.24.0
, you can update to any version in the 2.x series, but 3.x versions may break compatibility.
What is pip?
pip
is Python’s official package installer, used to download and manage packages from PyPI and other repositories. It comes pre-installed in recent Python versions.
Checking if pip is Installed
On Windows, open the terminal (Command Prompt or PowerShell) and type:
pip --version
On macOS or Linux, use:
pip3 --version
If pip
is not installed, you can download it by following the instructions on the official Python website.
Installing Packages with pip
Installing a Package
Use the following command to install a package:
pip install <package_name>
Example: To install the requests package:
pip install requests
Installing a Specific Version
To install a specific version of a package:
pip install <package_name>==<version>
Example:
pip install requests==2.20.1
Installing Multiple Packages
You can install multiple packages at once by specifying their names separated by spaces:
pip install requests flask numpy
Installing Packages from a File
You can list the required packages and versions in a requirements.txt
file and install them with:
pip install -r requirements.txt
Managing Installed Packages
Listing Installed Packages
To list installed packages:
pip list
Example output:
Package Version
---------- -------
pip 20.2.4
requests 2.24.0
flask 1.1.2
Updating Packages
To check which packages are outdated:
pip list --outdated
To update a package:
pip install --upgrade <package_name>
Uninstalling Packages
To remove a package:
pip uninstall <package_name>
Example:
pip uninstall requests
Managing Dependencies
When you install a package, pip
also installs the necessary dependencies automatically. To check the dependencies of a specific package:
pip show <package_name>
Example output for the requests package:
Name: requests
Version: 2.24.0
Requires: certifi, chardet, idna, urllib3
Using pip in Projects
Example: Making an HTTP Request with requests
After installing the requests package, you can use it directly in your projects:
import requests
response = requests.get('https://pypi.org/')
print(response.status_code)
Output:
200
Managing Virtual Environments
To avoid conflicts between packages in different projects, it is recommended to use virtual environments:
python -m venv myenv
For Linux/macOS:
source myenv/bin/activate
For Windows:
myenv\Scripts\activate
After activating the environment, you can use pip
to manage packages specific to that environment.
Summary
-
PyPI is a central repository for Python packages.
-
pip is used to install and manage packages from PyPI.
-
You can easily list, update, and uninstall packages.
-
Virtual environments help isolate dependencies between projects.
With this knowledge, you can efficiently explore the Python ecosystem and confidently manage dependencies in your projects.
Copyright Notice: Unless otherwise indicated, all articles are original to this site, and reproduction must cite the source
Article link:http://pybeginners.com/article/managing-packages-in-python-with-pip/