What installing a module means and why you need to do it

A module in Python is a file containing code that someone else wrote and packaged up so you can use it in your own programs. Installing a module means downloading that code to your computer and putting it in a place where Python can find it when you need it. Without installation, Python will not know the module exists and your program will crash with an error.

Most modules live in a central repository called PyPI (Python Package Index). When you install a module, a tool called pip (which stands for "pip installs packages") downloads it from PyPI and sets it up on your machine. Pip comes built into Python on most systems, so you likely already have it.

The most common reason you will need to install a module is because you found code online that uses it, or because a tutorial or project told you to. For example, if you want to work with dates and times beyond what Python's built-in tools offer, you might install the dateutil module. If you want to make web requests, you might install requests.

Key Takeaways

  • Pip is the standard tool for installing Python modules and comes with Python on most systems — you do not need to download it separately.
  • The command to install a module is pip install module_name, typed into your terminal or command prompt, not into Python itself.
  • On some computers, you may need to use pip3 instead of pip, or python -m pip install if pip is not found.
  • After installation, you use the module in your code by typing import module_name at the top of your Python file.
  • If an installation fails, the error message usually tells you what went wrong — most often a typo in the module name or a permission issue on your computer.

Check that pip is installed and working

Before you install anything, confirm that pip is on your system and can run. Open your terminal (on Mac or Linux) or command prompt (on Windows) and type this command exactly:

pip --version

If pip is installed, you will see a response like pip 23.1.2 from /usr/local/lib/python3.11/site-packages/pip (python 3.11). The exact version number does not matter — you just need to see that pip exists. If you see command not found or 'pip' is not recognized, try this instead:

pip3 --version

If pip3 works but pip does not, use pip3 for all the commands in this guide. If neither works, Python may not be installed on your system, or it was installed in a way that did not include pip. The fastest fix is usually to reinstall Python from python.org and make sure you check the box that says "Add Python to PATH" during installation.

Install a module using the pip install command

Once you have confirmed pip works, installing a module takes one command. Open your terminal or command prompt and type:

pip install module_name

Replace module_name with the actual name of the module you want. For example, to install the requests module (used for downloading web pages), you would type:

pip install requests

Pip will download the module and any other modules it depends on, then put them in a folder on your system where Python looks for modules. This usually takes a few seconds to a minute depending on the module size and your internet speed. You will see text scrolling in your terminal as it works. When it finishes, you will see a line that says Successfully installed requests or similar.

Do not type this command inside Python itself — type it in your terminal or command prompt before you open Python. This is a common mistake. If you accidentally typed pip install requests inside the Python interpreter, you will get an error. Exit Python first (by typing exit()) and then run the command in your terminal.

Verify the module installed and import it into your code

After pip says the installation succeeded, you can check that the module actually works by opening Python and importing it. Type python or python3 in your terminal to start the Python interpreter, then type:

import requests

If Python does not give you an error, the module is installed and ready to use. If you see ModuleNotFoundError: No module named 'requests', the installation did not work — scroll down to the troubleshooting section.

To use the module in a real Python file, put the import statement at the very top of your file, before any other code. For example, a file that uses the requests module might look like this:

import requests response = requests.get('https://example.com') print(response.status_code)

Every time you write a new Python file that needs a module, put the import at the top. Python will look in the folder where pip installed the module and load it automatically.

Install a specific version of a module

Most of the time, you want the newest version of a module, and pip install module_name will get it. Sometimes, though, code you are using requires an older version. You can tell pip which version to install by adding == and the version number:

pip install requests==2.28.0

This installs version 2.28.0 of requests instead of the latest. If you are not sure which version you need, check the documentation or instructions for the code you are trying to run — they usually say something like "requires requests 2.28 or higher".

You can also install a version range. For example, pip install requests>=2.28.0 installs version 2.28.0 or any newer version. The symbols work the same way as in math: >= means "greater than or equal to", <= means "less than or equal to", and == means "exactly this version".

Troubleshoot installation errors

If pip install fails, the error message usually points to the problem. Here are the most common ones:

ModuleNotFoundError after installation: This usually means pip installed the module, but Python cannot find it. Try closing your terminal completely and opening a new one, then try importing again. Sometimes Python needs to restart to see newly installed modules. If that does not work, try python -m pip install module_name instead of just pip install module_name — this tells Python to run pip as a module, which sometimes fixes path issues.

Permission denied error: This means pip does not have permission to write to the folder where modules are stored. On Mac or Linux, try adding --user to the end of your command: pip install --user module_name. This installs the module just for your user account instead of system-wide. On Windows, try running your terminal as administrator (right-click the command prompt and select "Run as administrator").

Could not find a version that satisfies the requirement: This usually means you typed the module name wrong. Check the spelling against the PyPI website or the instructions you are following. Module names are case-sensitive and sometimes use hyphens or underscores in unexpected places. For example, the module is called beautifulsoup4, not beautifulsoup.

No module named pip: This means pip is not installed. Reinstall Python from python.org, making sure to check "Add Python to PATH" during setup. After reinstalling, restart your terminal and try again.

Manage multiple modules with a requirements file

If you are working on a project that uses many modules, typing pip install for each one gets tedious. Instead, you can create a file called requirements.txt that lists all the modules your project needs. Each module name goes on its own line:

requests==2.28.0 beautifulsoup4==4.11.1 dateutil==2.8.2

Then, instead of installing each module one at a time, you run a single command:

pip install -r requirements.txt

Pip will read the file and install all the modules listed, with the exact versions you specified. This is especially useful if you are sharing your code with someone else — they can use the same requirements.txt file to install the exact same versions you used, which prevents problems from version differences.

You can also create a requirements.txt file from modules you have already installed by running pip freeze > requirements.txt. This command lists every module currently installed on your system and saves it to the file. You can then edit the file to remove modules you do not need.

Frequently Asked Questions

What is the difference between pip and pip3?

On systems with both Python 2 and Python 3 installed, pip usually points to Python 2 and pip3 points to Python 3. Since Python 2 is no longer supported, you should use pip3 if both are available. If only pip works on your system, it is fine to use it — it is pointing to Python 3.

Can I install a module from somewhere other than PyPI?

Yes, but it is less common. You can install directly from a GitHub repository using pip install git+https://github.com/user/repo.git, or from a local file using pip install /path/to/module.tar.gz. Most modules you will need are on PyPI, so start there first.

Do I have to reinstall modules every time I restart my computer?

No. Once pip installs a module, it stays on your computer until you uninstall it. Restarting your computer does not remove modules. You only need to install a module once per Python installation.

How do I uninstall a module I no longer need?

Use the command pip uninstall module_name. Pip will ask you to confirm, then remove the module from your system. This frees up disk space and keeps your system clean if you have modules you are no longer using.

What if a module name has a hyphen or underscore?

When you install, use the name exactly as it appears on PyPI — usually with hyphens. For example, pip install python-dateutil. When you import it in your code, replace hyphens with underscores: import dateutil. The PyPI page for the module always shows both the install name and the import name, so check there if you are unsure.