What Python modules are and why you need them

A Python module is a file containing code someone else wrote that you can use in your own programs. Instead of writing everything from scratch, you read a module and import it — a single line of code that brings in hundreds or thousands of lines of functionality.

Most modules live in a central repository called PyPI (Python Package Index). To get them onto your computer, you use a tool called pip, which is a package manager — it finds the module you want, downloads it, and installs it in the right place so Python can find it when you need it.

pip comes built into Python on most systems, so you likely already have it. The process is straightforward: open a terminal or command prompt, type a single command, and wait a few seconds. This guide walks you through exactly what to type and what to expect.

Key Takeaways

  • pip is the standard tool for installing Python modules and comes with Python by default on Windows, macOS, and Linux.
  • The basic command is pip install module_name, typed into a terminal or command prompt, not into Python itself.
  • You can check if pip is installed by typing pip --version, which shows the version number if it is present.
  • If a module fails to install, the error message usually tells you what went wrong — missing dependencies, permission issues, or a typo in the module name.
  • Virtual environments let you install modules for one project without affecting others, and are worth setting up if you work on multiple Python projects.

Checking that pip is installed

Before you install anything, confirm that pip is on your system. Open a terminal (on macOS or Linux) or Command Prompt (on Windows), and type this:

pip --version

If pip is installed, you will see a line like "pip 23.1.2 from /usr/local/lib/python3.11/site-packages/pip (python 3.11)". The exact numbers do not matter — you just need to see that pip exists. If you see "command not found" or "pip is not recognized", skip ahead to the troubleshooting section.

On some systems, especially older ones, you may need to type pip3 instead of pip. If pip --version fails but pip3 --version works, use pip3 for all the commands in this guide.

Installing a module with a single command

Once you know pip is there, installing a module takes one line. Type this into your terminal or command prompt:

pip install requests

Replace requests with the name of the module you actually want. (requests is a popular module for downloading web pages, so it is a good one to test with.) Press Enter and wait — pip will read the module and everything it depends on, then install them all.

You will see text scrolling past. When it stops and you get your prompt back, the installation is done. You should see a line near the end that says "Successfully installed" followed by the module name and version number.

Now you can use that module in your Python code by typing import requests at the top of your script. Python will find it because pip put it in the standard location where Python looks for modules.

Installing multiple modules at once

If you need several modules, you can install them all in one command by listing them with spaces between:

pip install requests beautifulsoup4 flask

pip will read and install all three. This is faster than running three separate commands and is useful when you are setting up a new project.

You can also create a text file (usually called requirements.txt) that lists all the modules your project needs, one per line. Then type:

pip install -r requirements.txt

This is the standard way teams share projects — instead of telling someone "install these eight modules", you give them the requirements file and they run one command. Many projects on GitHub include a requirements.txt for exactly this reason.

Understanding virtual environments

A virtual environment is a separate folder on your computer where Python and all your modules live, isolated from everything else. This matters if you work on multiple projects, because one project might need version 1.0 of a module while another needs version 2.0 — virtual environments let both exist without conflict.

Creating a virtual environment takes two commands. First, navigate to your project folder in the terminal, then type:

python -m venv env

This creates a folder called env (you can name it anything). Then set up it:

On macOS or Linux: source env/bin/set up

On Windows: env\Scripts\set up

Your prompt will change to show the environment name in parentheses, like (env) $. Now when you type pip install, modules go into this environment only. When you are done working, type deactivate to exit the environment.

Virtual environments are optional for learning, but they become essential once you have more than one project. They prevent the "it worked yesterday" problem where a module update breaks something you were not expecting.

What to do when installation fails

Most of the time, pip install module_name just works. When it does not, the error message usually points to the problem. Here are the most common ones:

ModuleNotFoundError or No module named — You typed the module name wrong, or it does not exist on PyPI. Check the spelling and try searching PyPI.org to confirm the exact name. Some modules have hyphens in their display name but underscores in the install name, or vice versa.

Permission denied — You do not have permission to write to the folder where pip wants to install. On macOS or Linux, try adding --user to the end: pip install --user module_name. On Windows, this usually means running Command Prompt as Administrator (right-click and choose "Run as administrator").

ERROR: Could not find a version that satisfies the requirement — The module exists, but not for your version of Python. Check the module's documentation to see which Python versions it supports. You may need to update Python or use a different module.

If the error message mentions a missing compiler or build tools, the module contains code that needs to be compiled for your system. This is rare for beginners, but when it happens, the error usually tells you exactly what to install.

Updating and removing modules

Modules get updates. To upgrade a module to the newest version, type:

pip install --upgrade module_name

Or the shorter version: pip install -U module_name

To see which modules you have installed and their version numbers, type:

pip list

To remove a module you no longer need, type:

pip uninstall module_name

pip will ask you to confirm before deleting it. This is useful for cleaning up after a project ends or if you installed something by mistake.

Frequently Asked Questions

Do I need to install pip separately?

No. pip comes with Python on Windows, macOS, and Linux. If you installed Python from python.org or used a package manager, pip is already there. The only exception is very old Python versions (before 3.4), which you should not be using anyway.

What is the difference between pip and pip3?

On systems with both Python 2 and Python 3 installed, pip usually refers to Python 2 (which is no longer supported) and pip3 refers to Python 3. If you only have Python 3, both commands do the same thing. Use whichever one works on your system.

Can I install modules from somewhere other than PyPI?

Yes, but it is not common for beginners. You can install from a GitHub repository or a local file, but PyPI is the standard place and where almost every module you will need lives. Stick with PyPI unless a module's documentation tells you otherwise.

What happens if I install a module and then update Python?

Your modules stay installed, but they live in the old Python version's folder. When you update Python, you may need to reinstall them for the new version. This is another reason virtual environments are useful — they make it straightforward to start fresh with a new Python version.

Is it safe to install modules from PyPI?

PyPI has some security checks, but not every module is reviewed before upload. Stick to modules that are well-known, have recent updates, and have many downloads. If you are unsure, check the module's GitHub page or documentation to see if it is actively maintained.