Personal Wiki
Python Tools
Pip

PIP

PIP is the package installer for Python. It is a command-line tool that allows you to easily install, upgrade, and manage Python packages and their dependencies.

Installation

PIP comes pre-installed with Python versions 3.4 and above. If you have an older version of Python or want to upgrade to the latest version of PIP, you can use the following command:

python -m ensurepip --upgrade

Alternatively, you can install PIP using the get-pip.py script. First, download the script by running the following command:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Then, execute the script to install PIP:

python get-pip.py

Usage

PIP provides a simple and straightforward interface for managing Python packages. Here are some common commands and their usage:

  • Search for a package:

    pip search search_query

    This command searches the Python Package Index (PyPI) for packages matching the provided query.

  • Install a package:

    pip install package_name

    This command installs the specified package and its dependencies.

  • Install a specific package version:

    pip install package_name==1.2.3

    This command installs a specific version of a package. Replace 1.2.3 with the desired version number.

  • Upgrade a package:

    pip install --upgrade package_name

    Use this command to upgrade an already installed package to the latest version.

  • Uninstall a package:

    pip uninstall package_name

    This command removes the specified package from your system.

  • List installed packages:

    pip list

    Use this command to display a list of all installed packages.

  • Show information about a package:

    pip show package_name

    This command displays detailed information about a specific package, such as version, location, and dependencies.

  • Check package dependencies:

    pip check

    Use this command to check the installed packages for any conflicts or missing dependencies.

  • Freeze installed packages:

    pip freeze > requirements.txt

    This command generates a requirements.txt file listing all installed packages and their versions.

  • Install packages from a requirements file:

    pip install -r requirements.txt

    If you have a requirements.txt file containing a list of packages and their versions, you can use this command to install them all at once.

Virtual Environments

PIP works seamlessly with Python virtual environments, which are isolated environments that allow you to manage packages independently for different projects. To create a virtual environment, use the following command:

python -m venv myenv

This creates a new virtual environment named myenv. To activate the virtual environment, run:

  • On Linux and macOS:

    source myenv/bin/activate
  • On Windows:

    myenv\Scripts\activate

Once activated, you can use PIP to install packages, and they will be isolated within the virtual environment, keeping your system Python installation clean.

To deactivate the virtual environment and return to your system's default Python environment, simply run the following command:

deactivate

Further Reading

These are just a few examples of what you can do with PIP. For more information and a complete list of commands, you can refer to the official PIP documentation (opens in a new tab).

;