When your code depends other Python libraries to run, you'll want to reach for pip
and venv
to install those third-party Python packages.

Table of contents
- Our program depends on a third-party package
- Installing packages using
pip
- Avoid installing packages globally
- Another project with a third-party dependency
- Creating a new virtual environment
- Installing packages into a virtual environment
- Other Python package management tools
- Use
pip
andvenv
when installing Python packages
Our program depends on a third-party package
Here we have a program called name.py
:
importrequestsprint(requests.get("https://pseudorandom.name").text.strip())
importrequestsprint(requests.get("https://pseudorandom.name").text.strip())
This program doesn't work right now:
$ python3 name.py
Traceback (most recent call last):
File "/home/trey/name.py", line 1, in<module>
import requests
ModuleNotFoundError: No module named 'requests'
$ python3 name.py
Traceback (most recent call last):
File "/home/trey/name.py", line 1, in<module>
import requests
ModuleNotFoundError: No module named 'requests'
It doesn't work because it uses the requests
module, which isn't included with Python.
This is a popular module that's included in the requests
package, that you can install from the Python Package Index by using a tool called pip
which comes bundled with Python.
Installing packages using pip
We can use pip
by …