Pip is a package manager for the Python programming language. It provides an easy-to-use command-line interface for installing, updating, and removing Python software. For example, to install the popular Requests HTTP client with pip, you could run the command:
pip install requests
In this article, we’ll outline how Python pip works, show you how to check whether it is installed, and explain how to install pip on Windows and macOS.
How Does PIP Work in Python?
Pip is a command-line client for the PyPI package index, the official repository for Python software. Python has a rich standard library, but developers often need to install packages that provide functionality not included in the default distribution. Pip makes it easy to manage packages and their dependencies and, with the requirements.txt file, install all of the packages required by a project.
PyPI indexes over a quarter of million Python packages. With pip, you can search PyPI, install packages along with their dependencies, update packages and dependencies, and remove packages you no longer need.
We have already seen how to install a package with pip. If you changed your mind, you could uninstall Requests with the command:
pip uninstall requests
You can see which Python packages you have installed with:
pip list
To find more information about a particular package, display its metadata with:
pip show requests
To see a list of available commands, run:
pip --help
PIP and Python Versions
In the previous examples, we used the pip command, but you may prefer to use pip2 or pip3 depending on your operating system’s Python environment and the version of Python your project uses. On some systems, the pip command is a soft link to a version-specific installation of pip, and it may not link to the version you expect.
For example, on macOS, the default installation of Python is rather old. If you use brew or some other method to install a newer Python, verify that the pip command invokes the correct version with:
pip --version
You can continue to use pip2 or pip3, or install the pyenv tool (or pyenv-win) to switch between versions. Pyenv helps you to install and manage multiple Python versions while ensuring that pip invokes the correct one.
How Do I Know if PIP is Installed?
Typically, pip is installed when you install newer versions of Python (Python 2.7.9+ and Python 3.4+) and virtualenv or pyenv. You can verify that pip is installed on macOS with:
pip --version
Or on Windows with:
pip -V
As we mentioned in the previous section, use pip2 and pip3 with the above command to invoke a specific version.
PIP and Python Versions
In the previous examples, we used the pip command, but you may prefer to use pip2 or pip3 depending on your operating system’s Python environment and the version of Python your project uses. On some systems, the pip command is a soft link to a version-specific installation of pip, and it may not link to the version you expect.
For example, on macOS, the default installation of Python is rather old. If you use brew or some other method to install a newer Python, verify that the pip command invokes the correct version with:
pip --version
You can continue to use pip2 or pip3, or install the pyenv tool (or pyenv-win) to switch between versions. Pyenv helps you to install and manage multiple Python versions while ensuring that pip invokes the correct one.
How Do I Know if PIP is Installed?
Typically, pip is installed when you install newer versions of Python (Python 2.7.9+ and Python 3.4+) and virtualenv or pyenv. You can verify that pip is installed on macOS with:
pip --version
Or on Windows with:
pip -V
As we mentioned in the previous section, use pip2 and pip3 with the above command to invoke a specific version.
Installing PIP
If you are using an older version of Python, you can install pip on Windows or macOS using a Python script provided by the PyPa project, as described below.
Installing PIP on macOS
As a general rule, you should not use the system Python on macOS. It is outdated and only included in the OS for compatibility reasons. We recommend that you use brew or pyenv to install a newer version. These tools will also install the relevant version of pip.
If pip is not installed alongside Python, follow the instructions below, but be aware that the get-pip.py script may cause compatibility problems with existing Python installations.
Run the following command to download the installation script:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
It’s good practice to examine scripts downloaded from the internet, so open get-pip.py in a text editor and satisfy yourself that it does nothing untoward. Then, execute it with:
python get-pip.py
Pip will be installed with default options. If you would like to adjust the configuration options, take a look at Pip’s documentation for more information.
Installing PIP on Windows
If you use Python in Windows Subsystem for Linux, install pip with your Linux distribution’s package manager. For example, if you use Ubuntu 18.04 with WSL, install pip with apt:
sudo apt install python3-pip
To install pip on Windows outside of WSL, first ensure that you have installed Python by running the following command at a command prompt:
python --v
Check to see if pip is already installed:
pip -V
If not, we’ll use the get-pip.py script. Download the script and open a command prompt at the folder you saved the script in.
python get-pip.py
Once the installation is complete, you can verify that pip was installed correctly with “pip -V”.
How To Uninstall Pip
The most straightforward way to uninstall pip on macOS or Windows is to use its own uninstall command:
pip uninstall pip
Alternatively, you can uninstall pip and dependencies by running pip as a module:
python -m pip uninstall pip setuptools
Keep in mind that this will remove pip and setuptools for whichever version of Python is invoked with the python command.