Pip Package Manager
In Python, packages are collections of modules used to group related code together and share it with others. There are two types of packages in Python: built-in and external. Built-in packages come pre-installed with Python, while external packages must be installed separately using the pip package manager.
With pip, users can easily install, upgrade, remove packages, and manage dependencies, making it a valuable tool for Devs and data scientists. Pip seamlessly integrates with the Python Package Index (PyPI), the world’s largest public repository of Python packages, and provides a way to connect to other package repositories (local or remote). With this, users can easily access and install the latest packages from a single centralized platform, saving time and effort in the package discovery and installation process.
Quick Installation Guide
Verifying the existence of pip in your system is a crucial step before installing packages. Pip is commonly included in most Python installations, but you can check its availability by running the following command:
python --version
In case you don’t have pip installed, don’t worry. You can easily install it by following these simple steps:
- Download the “get-pip.py” file to your computer by visiting the following link: https://bootstrap.pypa.io/get-pip.py
- Store the file in the same directory as python is installed.
- Open the cmd terminal and navigate to the directory where you saved the “get-pip.py” file.
- Run the following command: python get-pip.py
- The package should now be installed on your system, and you can verify it by running the following command: pip –version
Pip Fundamentals
Commands and Options
To use pip, you can run commands in the command line using the following format:
pip <command> [options]
You can access a list of available commands and options by typing “pip help” in the terminal.
Commands: install Install packages. download Download packages. uninstall Uninstall packages. freeze Output installed packages in requirements format. inspect Inspect the python environment. list List installed packages. show Show information about installed packages. check Verify installed packages have compatible dependencies. config Manage local and global configuration. search Search PyPI for packages. cache Inspect and manage pip's wheel cache. index Inspect information available from package indexes. wheel Build wheels from your requirements. hash Compute hashes of package archives. completion A helper command used for command completion. debug Show information useful for debugging. help Show help for commands. …
To see a list of packages, run the command below. It will display a list of all the packages installed in the current environment, along with their respective versions. This information can help manage package dependencies and ensure that you have the correct versions of packages installed for your project.
pip list
A similar command to “pip list” is “pip freeze,” which is also used for creating a “requirements.txt” file.
The “requirements.txt” file contains a list of package names and version numbers. This file enables you quickly install all the necessary packages for a project in a single command. Adhering to this practice makes it simple for other developers to install all the required Python modules and packages when they clone or checkout your project.
To create a “requirements.txt” file, you should use the following command:
pip freeze > requirements.txt
Installing Frameworks/Packages/Libraries
Installing packages with pip is a straightforward process. Run a command in your terminal or command prompt in the following format:
pip install <package_name>
Replace “<package_name>” with the name of the package you want to install. For example, to install the numpy, you would run:
pip install numpy
You can also select a specific package version by defining it along with the package name.
pip install <packagename>==v.v
For example, to install version 3.1.13 of the Django package, you would run the command:
pip install django==3.1.13
Alternatively, when working on a project, managing multiple dependencies and packages is often necessary. In these cases, using the “requirements.txt” file can be helpful. To install the packages listed in this file, run the command:
pip install -r requirements.txt
It will install all the packages listed in the txt file according to their specified versions. By using this file, you can ensure that your project environment is consistent across different machines and platforms.
On the other hand, to install a package from a repository other than the PyPI, such as Github, you can type:
pip install git+https://github.com/<username>/<repository>.git
Replace “<username>” with the GitHub username and “<repository>” with the repository name containing the package you want to install.
pip install git+https://github.com/example_user/example_package.git
You can also specify a branch or tag of the repository by adding “@<branch_or_tag_name>” to the end of the URL. For our previous example, to install version 1.0 of the package, you would run:
pip install git+https://github.com/example_user/example_package.git@1.0
Finally, to remove a package from your Python environment, you can utilize the “pip uninstall” command:
pip uninstall --yes <packagename>
Keep in mind that uninstalling a package may also remove any dependencies installed. You can use the “–yes” or “-y” flag to confirm the uninstallation without being prompted.
Installing Packages in Jupyter Notebook
When using Jupyter Notebook, users must employ the “!pip” command in a code cell. The “!” symbol denotes the characteristic format to run shell commands in Jupyter Notebook. For example, to install the matplotlib package, you should run the command:
!pip install matplotlib
Once the installation is complete, you have to import the package.
import numpy as np
Pip and Conda
Conda is an open-source and language-agnostic package manager. It is included in the Anaconda Python Distribution and offers various commands for managing packages and environments. Type the following code into the anaconda prompt to view a list of packages already installed in your system.
conda list
Use the Anaconda web navigator to find a package’s installation command. For example, to install the Flask library, you should run the command:
conda install -c anaconda flask
However, not all packages are available in the conda repository (e.g., sty); in that case, you will have to navigate into the PyPI, where you will find the commands.
To learn more about pip, please refer to its official documentation, which provides a comprehensive guide on how to use the tool. The documentation covers a wide range of topics, including how to install packages, manage dependencies, and create virtual environments.
Highlights
Project Background
- Project: pip
- Author: Ian Bicking
- Initial Release: 2011
- Type: Package Management System
- License: MIT
- Contains: command-line interface, distributions of python
- Language: Python
- GitHub:/pypa/pip
- Runs On: OS-agnostic
- Twitter: /pypi
Main Features
- Users can use pip to easily install, uninstall, upgrade, and remove packages for Python.
- Pip automatically manages the dependencies of packages you have installed.
- Users can easily upgrade or downgrade packages as needed by specifying the package version.
- Pip integrates well with virtual environments.
- Pip is designed to work with the PyPI repository, which contains thousands of packages for Python.
- Pip has a straightforward syntax.
Prior Knowledge Requirements
- Users should understand how to navigate the file system and run commands from the terminal or command prompt.
- Users should have a basic understanding of the Python programming language.
- Users should know how to create and manage virtual environments and how they are used in Python.
Community Benchmarks
- 8,500 Stars
- 2,800 Forks
- 600+ Code contributors
- Source: pip GitHub
References
[1] Documentation. https://pip.pypa.io/en/stable/
[2] Anaconda. https://anaconda.org/