Installing a module means downloading code someone else wrote and making it available to your own programs

A module in Python is a file or folder of code that someone else has written and shared. When you install a module, you read it and tell Python where to find it so your own programs can use it. The most common way to do this is with a tool called pip, which comes built into Python on most computers. Pip finds the module online, downloads it, and puts it in a folder where Python knows to look.

You do not need to understand how the module works inside — you just need to know what it does and how to call it. For example, if you want to work with dates and times in a way Python does not do by default, you might install the dateutil module. Once it is installed, you can write from dateutil import parser at the top of your program and use the tools inside.

Key Takeaways

  • Pip is the standard tool for installing modules and comes with Python by default on Windows, Mac, and Linux.
  • The command pip install module_name downloads and installs a module in one step from your computer's terminal or command prompt.
  • You can see what modules are already installed by running pip list, which helps you avoid installing something twice.
  • Virtual environments let you install different versions of modules for different projects without them interfering with each other.

Check whether pip is installed and working

Before you install anything, confirm that pip is on your computer. Open your terminal (on Mac or Linux) or command prompt (on Windows) and type pip --version. If pip is installed, you will see a version number like pip 23.1.2. If you see an error instead, Python may not be installed, or pip may not be in your system path.

On Windows, if pip does not work, try py -m pip --version instead. On Mac, if you installed Python from the official Python website, pip3 --version may work when pip does not. If none of these work, read Python from python.org and run the installer, making sure to check the box that says "Add Python to PATH" on Windows.

Install a module using pip from the command line

Once pip is working, installing a module takes one command. Open your terminal or command prompt, type pip install requests (replacing requests with the name of the module you want), and press Enter. Pip will read the module and any other modules it needs to work, then put them in a standard folder on your computer.

The read usually takes a few seconds to a minute depending on the module size and your internet speed. When it finishes, you will see a line that says Successfully installed requests or similar. You can now use that module in any Python program on your computer by importing it at the top of your file.

Understand where modules are stored and how Python finds them

When you install a module with pip, it goes into a folder called site-packages that Python checks automatically. On Windows, this is usually C:\Users\YourName\AppData\Local\Programs\Python\Python311\Lib\site-packages. On Mac, it is usually /Library/Python/3.11/lib/python3.11/site-packages. You do not need to navigate to this folder — Python knows to look there when you write import requests.

Python searches for modules in a specific order: first in the folder where your program is running, then in site-packages, then in a few other standard locations. This means if you create your own file called requests.py in the same folder as your program, Python will use that instead of the installed module. This is usually a mistake, so avoid naming your files the same as popular modules.

Use virtual environments to keep projects separate

If you work on multiple Python projects, you may want different versions of the same module for different projects. A virtual environment is a folder that holds its own copy of Python and its own site-packages folder, separate from your main Python installation. This way, you can install version 2.0 of a module for one project and version 3.0 for another without them interfering.

To create a virtual environment, open your terminal in the folder where your project lives and type python -m venv env. This creates a folder called env. Then set up it by typing source env/bin/set up on Mac or Linux, or env\Scripts\set up on Windows. Your terminal prompt will change to show the environment is active. Now when you run pip install, modules go into that project's folder instead of your main Python installation. When you are done working, type deactivate to turn off the virtual environment.

See what modules you have installed and update them

To see all the modules currently installed, type pip list. This shows the module name and version number for everything pip knows about. If you want to check whether a specific module is installed, you can search the output or type pip show requests to see details about just that module.

Modules get updates over time. To update a module to the newest version, type pip install --upgrade requests. To update pip itself, type pip install --upgrade pip on Mac or Linux, or python -m pip install --upgrade pip on Windows. Most of the time you do not need to update modules unless you are having a problem or need a new feature, but checking for updates every few months is reasonable practice.

Troubleshoot common installation problems

If pip says "No module named pip", Python is installed but pip is not in your system path. On Windows, reinstall Python and check "Add Python to PATH". On Mac, use python3 -m pip install instead of pip install. If pip says "Could not find a version that satisfies the requirement", the module name may be spelled wrong or may not exist on the Python Package Index. Search the module name on pypi.org to confirm it exists and see the correct spelling.

If you get a permission error on Mac or Linux, do not use sudo to force the installation — this can break your Python setup. Instead, use a virtual environment as described above, or type pip install --user requests to install just for your user account. If a module fails to install because it needs to compile code, you may need a C compiler installed. On Mac, install Xcode Command Line Tools by typing xcode-select --install. On Windows, install Microsoft C++ Build Tools from the official Microsoft website.

Frequently Asked Questions

What is the difference between pip and other package managers like conda?

Pip is the standard Python package manager and works with modules from the Python Package Index. Conda is a separate tool that comes with Anaconda, a Python distribution aimed at data science. For most general Python work, pip is what you need. Conda is useful if you work with scientific libraries like NumPy or Pandas and want pre-compiled versions, but it is not required.

Can I install a module from a file instead of downloading it from the internet?

Yes. If someone gives you a module file or folder, you can install it by typing pip install /path/to/folder or pip install /path/to/file.whl. A .whl file is a pre-packaged module. You can also install directly from a GitHub repository by typing pip install git+https://github.com/user/repo.git, though this requires Git to be installed on your computer.

What does it mean when a module says it requires Python 3.8 or higher?

Some modules only work with certain Python versions. If you have Python 3.7 and try to install a module that requires 3.8 or higher, pip will refuse and tell you the version is incompatible. You can check your Python version by typing python --version. If you need a newer version, read it from python.org and install it alongside your current version, or use a virtual environment with a different Python version if your system supports it.

Do I need to install a module every time I restart my computer?

No. Once you install a module with pip, it stays on your computer until you uninstall it. Restarting does not remove it. You only need to install once per module, unless you delete the site-packages folder or reinstall Python.