- Introduction to PIP
- Installing and Uninstalling Packages
- Virtual Environments
- Managing Dependencies
- Publishing Packages
- Summary
- Tasks
PIP is the package installer for Python. It allows you to install and manage additional libraries and dependencies that are not included in the standard library.
You can check the version of PIP installed on your system using the following command:
pip --version
Output:
pip 21.0.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
PIP makes it easy to install and uninstall Python packages.
To install a package, use the pip install
command followed by the package name.
pip install requests
Output:
Collecting requests
Downloading requests-2.25.1-py2.py3-none-any.whl (61 kB)
Installing collected packages: requests
Successfully installed requests-2.25.1
To uninstall a package, use the pip uninstall
command followed by the package name.
pip uninstall requests
Output:
Found existing installation: requests 2.25.1
Uninstalling requests-2.25.1:
Would remove:
/usr/local/lib/python3.9/site-packages/requests-2.25.1.dist-info/*
/usr/local/lib/python3.9/site-packages/requests/*
Proceed (y/n)? y
Successfully uninstalled requests-2.25.1
Virtual environments allow you to create isolated Python environments for different projects. This helps to avoid conflicts between dependencies of different projects.
You can create a virtual environment using the venv
module.
python -m venv myenv
To activate the virtual environment, use the following command:
- On Windows:
myenv\Scripts\activate
- On macOS and Linux:
source myenv/bin/activate
To deactivate the virtual environment, use the following command:
deactivate
You can manage the dependencies of your project using a requirements.txt
file. This file lists all the packages required for your project.
To create a requirements.txt
file, use the following command:
pip freeze > requirements.txt
To install the dependencies listed in a requirements.txt
file, use the following command:
pip install -r requirements.txt
You can publish your own Python packages to the Python Package Index (PyPI) so that others can install and use them.
Create a setup.py
file with the necessary information about your package.
from setuptools import setup, find_packages
setup(
name="mypackage",
version="0.1",
packages=find_packages(),
install_requires=[
# List your package dependencies here
],
)
To build your package, use the following command:
python setup.py sdist bdist_wheel
To upload your package to PyPI, use the twine
tool.
pip install twine
twine upload dist/*
In this chapter, we covered package management with PIP, including installing and uninstalling packages, creating and managing virtual environments, managing dependencies with requirements.txt
, and publishing packages to PyPI.
- Install a package using PIP and verify its installation.
- Create a virtual environment and activate it.
- Generate a
requirements.txt
file for a project and install the dependencies from it. - Create a simple Python package and publish it to PyPI.