Installing Packages in Python with pip
Python is a popular programming language, largely due to its rich ecosystem of libraries. To integrate these libraries, pip is commonly used — the standard tool for installing packages from the Python Package Index (PyPI).
What is pip?
pip (which stands for “Pip Installs Packages”) is a package manager that lets you install, update, and remove external Python libraries. If you are using Python 3.4 and above, pip is already installed by default, so no additional setup is needed.
To check if pip is installed, run this command in the terminal:
pip --version
If you have multiple versions of Python, use pip3:
pip3 --version
Installing Packages
To install a package, execute this command:
pip install package_name
For example, to install the library for working with HTTP requests, requests:
pip install requests
If you are using macOS or Linux and encounter permission errors, install the package as a superuser:
sudo pip install requests
Installing a Specific Version
Install a specific version of a package by specifying it after ==:
pip install requests==2.31.0
Operators such as >=, <=, ~= are also available to specify version ranges.
Installing from a requirements.txt File
When working on a project, save the list of dependencies in a requirements.txt file. To install all dependencies from it, use:
pip install -r requirements.txt
Updating and Removing Packages
To update a package to the latest version:
pip install --upgrade package_name
To remove a package:
pip uninstall package_name
Tips
- Always use virtual environments (such as
venv) to isolate project dependencies. - To view installed packages, use:
pip list
- To find out where a specific package is installed, use:
pip show package_name
Installing and managing packages in Python with pip is fundamental to productive work with the language’s ecosystem. By mastering these basic commands, you can leverage thousands of ready-made libraries and accelerate development significantly.