Learn to install and manage external Python packages using the Python Package Index (PyPI) and the pip tool.
In this lesson, you’ll learn how to install and manage external packages from the Python community using the centralized Python Package Index (PyPI). Maintained by the Packaging Working Group, PyPI is the primary repository for Python packages and is completely free, allowing you to install and use any available code.
To work with PyPI packages, you need to use a tool called pip. Before installing packages, make sure that pip is installed or upgraded. Some Python installations include pip by default, while others require manual installation. You can upgrade pip using the following command:
Copy
Ask AI
python -m ensurepip --upgrade
You can verify that pip is installed by checking its version:
Copy
Ask AI
pip --version
For instance, the output might be similar to:
Copy
Ask AI
pip 21.2.dev0 from /opt/virtualenvs/python3/lib/python3.8/site-packages/pip (python 3.8)
A working network connection is required since pip downloads packages over the internet.
You can search for packages either directly on the PyPI website or via the pip search command in your terminal. Once you find a package you want to use, you can install it using the pip install command.For example, let’s install the popular community package Pygame. After installation, you can retrieve more details about the package (such as its version, description, author, and dependencies) using the pip show command:
Copy
Ask AI
~/QuarrelsomeRecklessTechnician$ pip --versionpip 21.2.dev0 from /opt/virtualenvs/python3/lib/python3.8/site-packages/pip (python 3.8)~/QuarrelsomeRecklessTechnician$ pip install pygameRequirement already satisfied: pygame in /opt/virtualenvs/python3/lib/python3.8/site-packages (2.0.1)WARNING: You are using pip version 21.2.dev0; however, version 21.3.1 is available.You should consider upgrading via the '/opt/virtualenvs/python3/bin/python3 -m pip install --upgrade pip' command.~/QuarrelsomeRecklessTechnician$ pip show pygameName: pygameVersion: 2.0.1Summary: Python Game DevelopmentHome-page: https://www.pygame.orgAuthor: A community project.Author-email:[email protected]License: LGPLLocation: /opt/virtualenvs/python3/lib/python3.8/site-packagesRequires:Required-by:
Specifying Package Versions and Uninstalling Packages
With pip, you can also install a specific version of a package by appending == and the desired version number to the package name. For example, to install version 2.0.1 of Pygame, run:
Copy
Ask AI
~/QuarrelsomeRecklessTechnician$ pip install pygame==2.0.1Collecting pygame==2.0.1 Using cached pygame-2.0.1-cp38-cp38-manylinux1_x86_64.whl (11.8 MB)Installing collected packages: pygame Attempting uninstall: pygame Found existing installation: pygame 2.0.2 Uninstalling pygame-2.0.2: Successfully uninstalled pygame-2.0.2Successfully installed pygame-2.0.1WARNING: You are using pip version 21.2.1; however, version 21.3.1 is available.You should consider upgrading via the '/opt/virtualenvs/python3/bin/python3 -m pip install --upgrade pip' command.~/QuarrelsomeRecklessTechnician$
If you no longer require a package, you can easily remove it using the following command:
Copy
Ask AI
pip uninstall <package-name>
Replace <package-name> with the name of the package you wish to uninstall.That’s it for now! It’s time to gain some hands-on experience with installing and managing Python packages from PyPI. Happy coding!