Table of Contents
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 --versionYou 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:
- Using
python -m pip(orpy -m pipon Windows) instead ofpip - Making sure Python was installed with the “Add to PATH” option (Windows)
Example:
python -m pip --version
or on Windows if python doesn’t work:
py -m pip --versionBasic `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 requestsOn Windows, this may be:
py -m pip install requestsAfter installing, you can use the library in your Python code:
import requestsInstalling 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.0Other useful version specifiers:
- Newer than or equal to a version:
pip install requests>=2.30.0- Between versions:
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 requestsShort form:
pip install -U requestsYou 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 listYou will see output like:
Package Version
---------- -------
pip 24.0
requests 2.31.0
setuptools 68.0.0To 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 requestsGetting information about a package
To see more details about an installed package:
pip show requestsThis will print:
- Version
- Install location
- Dependencies
- Homepage / project URL
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 flaskYou can also mix in versions:
pip install requests==2.31.0 flask>=3.0.0Using `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.0To install everything listed in the file:
pip install -r requirements.txtThis is especially useful when:
- Sharing a project with someone else
- Moving a project to a new computer
- Deploying a project to a server
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:
- For “the default” Python:
python -m pip install requests- If you use the
pylauncher on Windows:
py -m pip install requests- For a specific version with
py:
py -3.12 -m pip install requestsThis 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:
pip: command not found'pip' is not recognized as an internal or external command
Try:
python -m pip ...py -m pip ...(Windows)
For example:
python -m pip install requestsIf 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:
- Use a virtual environment for your project (often the best approach in real projects)
- Or install packages just for your user (not system-wide):
pip install --user requestsVirtual 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:
- Check that your internet connection works
- Ask your network administrator how to configure
pipwith your proxy
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:
- Install it with
pip. - Start Python (interactive mode).
- Try importing it.
Example:
pip install requests
pythonThen 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:
- Install
requests, import it in Python, and print its version. - Install a specific version of a package (for example,
pip install requests==2.31.0) and confirm it in Python. - Create a
requirements.txtfile with at least two packages and install them usingpip install -r requirements.txt. - List all installed packages with
pip listand 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.