Kahibaro
Discord Login Register

Installing external libraries with pip

Why install external libraries?

The Python standard library is powerful, but many useful tools live in external libraries created by other people. To use them, you need to install them first.

pip is the standard tool that downloads and installs these external libraries from the Python Package Index (PyPI), which is a huge online collection of Python packages.

This chapter focuses on how to use pip to install and manage these libraries.

Checking if `pip` is installed

In most modern Python installations, pip comes pre-installed.

Open your terminal (Command Prompt, PowerShell, Terminal app, etc.) and type:

pip --version

You might see something like:

pip 24.0 from C:\Python311\Lib\site-packages\pip (python 3.11)

On some systems, pip might be tied to Python 3 and called pip3:

pip3 --version

If you see a version number, pip is available and ready.

If you get an error like “pip is not recognized” or “command not found”, try:

Example:

python -m pip --version

or on Windows if python doesn’t work:

py -m pip --version

Basic `pip` install command

The core command to install a package is:

$$
\texttt{pip install package\_name}
$$

For example, to install the requests library:

pip install requests

If pip is not directly recognized, use the Python module form:

python -m pip install requests

On Windows, this may be:

py -m pip install requests

After installing, you can use the library in your Python code:

import requests

Installing a specific version

Sometimes you want a particular version of a library (for compatibility with code or tutorials).

Use == followed by the version number:

pip install requests==2.31.0

Other useful version specifiers:

  pip install requests>=2.30.0
  pip install "requests>=2.30.0,<2.32.0"

(Quotes help avoid issues with > and < in some shells.)

Upgrading an existing library

To upgrade a library to the latest version:

pip install --upgrade requests

Short form:

pip install -U requests

You can check the installed version in Python:

import requests
print(requests.__version__)

Uninstalling a library

If you no longer need a library, or want to remove a broken install:

pip uninstall requests

pip will ask you to confirm before removing it.

Listing installed packages

To see what libraries are currently installed:

pip list

You will see output like:

Package    Version
---------- -------
pip        24.0
requests   2.31.0
setuptools 68.0.0

To search for a specific package in the list:

pip list | find "requests"   # Windows (Command Prompt)

or on macOS/Linux with a typical shell:

pip list | grep requests

Getting information about a package

To see more details about an installed package:

pip show requests

This will print:

This is useful if you’re debugging or want to know what version you’re using.

Installing multiple packages at once

You can install several libraries in a single command by listing them:

pip install requests flask

You can also mix in versions:

pip install requests==2.31.0 flask>=3.0.0

Using `requirements.txt` files

Projects often use a requirements.txt file to record which packages (and versions) they need. This makes it easy to set up the same environment on another computer.

A simple requirements.txt could look like:

requests==2.31.0
flask>=3.0.0,<3.1.0

To install everything listed in the file:

pip install -r requirements.txt

This is especially useful when:

Using `pip` with `python` or `py`

On some systems, there are multiple Python versions installed (for example, Python 3.10 and 3.12). Each version has its own packages.

To make sure you’re using pip for a specific Python:

  python -m pip install requests
  py -m pip install requests
  py -3.12 -m pip install requests

This ensures that the package is installed for the correct Python interpreter.

Common `pip` problems and tips

“command not found” or “is not recognized”

If you see something like:

Try:

For example:

python -m pip install requests

If that works, continue using this style.

Permission or access errors

If you see errors about permission or access (especially on macOS/Linux), avoid using sudo at first. Instead:

  pip install --user requests

Virtual environments and deeper environment management are usually covered in more advanced material, but be aware that they exist to keep projects’ dependencies separate.

Proxy or network problems

If you are behind a corporate proxy or have connection issues, pip may fail to download packages. In that case, you may need to:

These setups are environment-specific and often outside Python itself.

Verifying that an installation worked

A quick way to confirm that a library installed correctly:

  1. Install it with pip.
  2. Start Python (interactive mode).
  3. Try importing it.

Example:

pip install requests
python

Then in Python:

import requests
print("Requests version:", requests.__version__)

If no error appears and you see a version number, the installation was successful.

Simple practice ideas

You can practice using pip with these small tasks:

  1. Install requests, import it in Python, and print its version.
  2. Install a specific version of a package (for example, pip install requests==2.31.0) and confirm it in Python.
  3. Create a requirements.txt file with at least two packages and install them using pip install -r requirements.txt.
  4. List all installed packages with pip list and pick one to uninstall, then reinstall it.

These exercises will make you comfortable with the basic pip workflow you will use in almost every real-world Python project.

Views: 17

Comments

Please login to add a comment.

Don't have an account? Register now!