The pip package manager in Python

0
(0)

 

A guide to installing pip and using basic commands for working with Python libraries.

The command is entered inside. A command like this won’t work here. First, you need to exit the interpreter with the command:

When developing Python programs, installing and updating libraries is essential. The pip package manager can help with this.

Pip is a command-line tool that installs, updates, and removes third-party libraries from the PyPI repository. Without it, you’d have to install libraries manually: search for the desired package on the website, download the archive, unzip it, copy the files to Python directories, and then manually monitor for updates.

How to tell if pip is installed

First, make sure you have Python installed. In modern versions of Python ( starting with 3.4 ), pip is built in by default, but sometimes you need to install it separately or make it visible to the system. Therefore, first, check that everything is installed and working correctly.

Checking via terminal or command line

Open a terminal or command prompt and enter:

pip –version

If pip is installed correctly, you will see a line like:

pip 24.0 from C:\…\site-packages\ pip ( python 3.12 )

Two things are important here: the pip version number and the Python version at the end of the line. This will determine which Python interpreter this pip is linked to.

Checking pip installation via Python commands

Pip may be installed, but not added to your environment variables. In this case, you can check it using Python. In the terminal or command line, enter:

python -m pip –version

or (if you have Python 3 ):

python3 -m pip -version

If the answer is yes, then pip is ready to use.

You can also add it to your environment variables to call commands directly through pip:

On Windows

  1. Find out where Python is located. The path looks something like this:
    C:\Users\Name\AppData\Local\Programs\Python\Python312\Scripts. The pip.exe executable file is located
    inside the Scripts folder.
  2. Copy the path to the Scripts folder.
  3. Open Environment Variables in System Settings.
365

Find the PATH variable for the user and add the copied path.

  1. Close and reopen Command Prompt.
  2. Check:
pip –version
  1. If the command works, you can install packages like this:
pip install package_name

On macOS and Linux

On these systems, pip is often located in the ~/.local/bin folder or in the directory where Python is installed.

  1. Perform:
python3 -m pip –version
  1. If the command works, add the pip folder to your PATH variable via your shell configuration file (.bashrc, .zshrc, etc.).
  2. After changing, restart the terminal and check:
pip3 –version

Pip not found

If you see the following message in response:

‘pip’ is not recognized as an internal or external command
or
command not found: pip

The system doesn’t find pip. Either it’s not installed, or:

  • Pip is tied to a different Python interpreter

Sometimes you have multiple versions of Python installed on your system, and pip isn’t the version you’re currently using. Try running different commands:

Windows

py -m pip –version
py -3 -m pip –version
pip3 –version
python -m pip –version

macOS and Linux

pip3 –version
python3 -m pip –version

If one of these commands shows the version of pip, then you need to install packages through it, for example:

pip3 install package_name
  • Pip is installed only in a virtual environment (venv)
    Then the command works within the virtual environment and does not work in the “global” system.

Windows

Go to the project folder where the venv folder is located and run:

venv\Scripts\activate

The environment name will appear at the beginning of the line. Now you can install libraries:

pip install package_name
pip list

To exit the environment, enter:

deactivate

If you need to install outside of the environment, run pip like this:

python -m pip install package_name

macOS and Linux

In the project directory, activate the environment:

source venv/bin/activate

Once activated, the pip command applies only to this environment:

pip install package_name
pip list

Escaping from encirclement:

deactivate

To install packages into the “global” Python, type:

python3 -m pip install package_name

Installing pip

If pip is not installed along with Python, you can install it manually.

On Windows

  1. Try installing pip via ensurepip.

Open cmd or PowerShell and type:

python -m ensurepip –upgrade

If the command runs without errors, check:

python -m pip -version

or:

pip –version

If the version is displayed, pip is installed.

  1. If ensurepip didn’t work.

In this case, the safest way is to reinstall Python using the official installer.

  1. After that, check again:
python -m pip –version

or

pip –version

On macOS

  1. Open the terminal.
  2. Check for Python:
python3 –version
  1. Try installing pip via ensurepip :
python3 -m ensurepip –upgrade
  1. If the command succeeded, check:
python3 -m pip –version

or

pip3 –version

If ensurepip says the module is missing and pip still doesn’t work, it’s best to install pip through the Homebrew package manager or reinstall Python and then check again.

python3 -m pip

On Linux

On Linux, Python is often already installed, but pip is installed separately through a package manager. First, enter the following in the terminal:

sudo apt update
sudo apt install python3-pip

Then check:

python3 -m pip –version

or

pip3 –version

How to work with pip

Pip commands are entered in the terminal or command line. The examples are the same for Windows, macOS, and Linux. On Linux and macOS, python3 and pip3 are often used instead of python, but the logic remains the same.

What does the input line look like?

  • Terminal or Command Prompt
    On Windows:
C:\Users\you >

On Linux and macOS:

you@pc:~$
  • Python Interactive Console
>>>

If the line starts with

>>>

Python.

pip install requests

 

exit ()

or Ctrl+Z and Enter on Windows, Ctrl+D on Linux and macOS.
After that, you can enter pip commands in the regular terminal.

pip commands

  • Installing libraries with pip install
requests

or

python -m pip install requests

The command downloads the requests package from PyPI and installs it into the selected Python installation.

365

Entering a command into cmd in Windows

You can also specify a specific version:

pip install requests== 2.31 . 0
pip install “requests>=2.30.0”

Such restrictions help to lock in a specific version of a library in a project.

  • Removing libraries with pip uninstall

To remove a package by name, enter:

pip uninstall requests

After the command, pip asks for confirmation. Usually, just enter y and press Enter.

How to remove a library in Python using pip
  • List packages and get information with pip list and pip show

You can view all installed libraries via:

pip list

The command outputs a table with package names and their versions.

Find out details about a specific library:

pip show requests

In response, we will receive information about the request package:

Name — package name

Version — current version

Location — path to installation

Requires – list of dependencies

  • Updating packages with pip install –upgrade

To update one library to the latest version, enter:

pip install –upgrade requests

Pip downloads the latest version of the package and installs it over the old one.

  • Working with multiple library versions

Sometimes, after updating a package, it doesn’t work as expected. To install a different version of the package (for example, requests ), run:

pip install “requests==2.30.0”

This command will remove the current version of the package and install the specified 2.30.0.

To remember which version your project is running, you can record it in the requirements.txt file. For example:

requests== 2.30. 0

This is necessary so that in the future we can install all the packages from this file in a new environment using the command:

pip install -r requirements. txt

What is a requirements.txt file?

This is a regular text file that can be opened in any editor ( VS CodeNotepad, etc.). It is created in the project folder.

The requirements.txt file is used to store project dependencies. Each line describes one package and its version, for example:

requests== 2.30. 0
numpy== 2.0. 1

How to install libraries from requirements.txt

To install all libraries from requirements.txt, open a terminal in the project folder and enter:

pip install -r requirements.txt

Pip will read the requirements.txt file, download the specified packages with the required versions, and add them to the current environment.

How to update pip itself

When a pip update is released, you can find out about it on the command line:

Python pip update notification

To do this, we will use the suggested command:

python.exe -m pip install –upgrade pip

Pip will install the update:

The convenience of using pip is that if errors occur, the error line will detail the action plan for resolving the issue. For example, if the issue is a package version conflict, pip will indicate the correct

365 version, which should be entered into requirements.txt.

How to view and clear the pip cache

The pip cache is a folder where pip stores downloaded library files. Most often, these are files with the .whl extension (also called “wheels”). A wheel is a pre-built version of a package. Pip organizes it into folders, making installation faster than from source.

You can view the path to the cache directory using the command:

pip cache dir

In response, pip will show the folder where it stores the “wheels” and archives.

How to clear cache

If installing libraries produces unspecified errors or the cache has grown significantly, you can clear it using the command:

pip cache purge

After this, pip will remove all saved files. The next time you install packages, it will take longer because pip will download them again from PyPI, but you’ll eliminate any potential issues with the old cache.

Pip and virtual environments

We briefly mentioned venv above, where pip might be installed in the environment but not visible on the global command line.

Typically, packages are installed not into the “global” Python installation, but into a separate virtual environmentThis ensures that dependencies of one project don’t interfere with others. 

In a virtual environment, pip commands only work within the project. To access it, enter the following commands:

Windows

venv\Scripts\activate

macOS and Linux

source venv/bin/activate

The environment name will appear at the beginning of the terminal line, for example:

( venv ) C:\Users\you\project >

Now you can enter regular commands:

pip install requests
pip list
pip uninstall requests

When the work is finished, you can exit the environment:

deactivate

After this, pip commands will again refer to the shared Python environment.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Explore More IT Terms


Share this term: Facebook X LinkedIn WhatsApp Email

Leave a Reply

Your email address will not be published. Required fields are marked *

Free ad network for.