How to Install Modules in Python
What you’ll build or solve
You’ll install a Python module with pip, confirm you installed it for the same Python you run, and fix the most common “installed but can’t import” issues.
When this approach works best
This approach works best when you need to:
Learn Python on Mimo
- Add a third-party package to a script, like
requestsfor HTTP orrichfor nicer terminal output. - Install a library for a quick experiment or learning project.
- Recreate someone else’s setup using a
requirements.txtfile.
Avoid this approach when:
- The module is part of the standard library, like
json,math, orpathlib. You don’t install those. - You need project isolation or multiple projects with different versions. Use a virtual environment instead.
Prerequisites
- Python 3 installed
- A terminal you can run commands in
- Basic comfort running Python, for example:
Bash
python -c "print('hi')"
Step-by-step instructions
1) Install a module with python -m pip
Use python -m pip so pip matches the Python you are running.
Bash
python -m pip install requests
If your system uses python3:
Bash
python3 -m pip install requests
What to look for:
If you have both python and python3, use the one you normally run your scripts with.
2) Verify the module works
Check that pip sees the package:
Bash
python -m pip show requests
Then import it directly:
Bash
python -c "import requests; print(requests.__version__)"
If this runs without errors and prints a version, the install worked.
3) Install modules from requirements.txt
Use this when a project already lists its dependencies.
Bash
python -m pip install -r requirements.txt
If you need to create the file from your current environment:
Bash
python -m pip freeze > requirements.txt
This writes all installed packages and versions to the file.
4) Upgrade or uninstall a module
Upgrade a package:
Bash
python -m pip install --upgrade requests
Uninstall a package:
Bash
python -m pip uninstall requests
Examples you can copy
1) Install a package and run a quick import check
Bash
python -m pip install rich
python -c "from rich import print; print('[green]Installed![/green]')"
2) Install exactly what a project needs
Bash
python -m pip install -r requirements.txt
python -c "import requests; import flask; print('imports ok')"
3) Upgrade a package to get a bug fix
Bash
python -m pip install --upgrade requests
python -c "import requests; print(requests.__version__)"
Common mistakes and how to fix them
Mistake 1: Using pip that points to a different Python
You might run:
Bash
pip install requests
python -c "import requests"
Why it breaks:
pip can be connected to a different Python than python, so the install goes to the wrong place.
Correct approach:
Bash
python -m pip install requests
python -c "import requests; print(requests.__version__)"
Mistake 2: Installing the wrong thing because the name is different
You might run:
Bash
python -m pip install pil
Why it breaks:
Some libraries install under a different package name than you expect.
Correct approach:
Bash
python -m pip install pillow
python -c "from PIL import Image; print('PIL import ok')"
Mistake 3: Trying to install a standard library module
You might run:
Bash
python -m pip install json
Why it breaks:
Standard library modules ship with Python. Installing a third-party package with the same name can cause confusion.
Correct approach:
Python
import json
data = json.loads('{"ok": true}')
print(data["ok"])
Troubleshooting
If you see ModuleNotFoundError right after installing, run these two commands and confirm they refer to the same Python:
Bash
python -c "import sys; print(sys.executable)"
python -m pip -V
If you see No module named pip, reinstall or enable pip:
Bash
python -m ensurepip --upgrade
python -m pip install --upgrade pip
If you hit a permissions error when installing, avoid system installs and use a virtual environment instead.
If you are behind a corporate network and installs fail, try upgrading pip first:
Bash
python -m pip install --upgrade pip
Quick recap
- Install with
python -m pip install packageto matchpipto your Python. - Verify with
python -c "import package"and print a version if available. - Install a full dependency list with
python -m pip install -r requirements.txt. - Upgrade with
-upgrade, remove packages withpip uninstall. - Use a virtual environment for project isolation when needed.
Join 40M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot