Where are the Python Modules Stored



What are Python Modules?

Python module is a file containing Python functions, variables, constants, and objects with a ".py" extension. This file can be imported inside another program to reuse its functionality.

It is quite helpful to understand where the module is stored, some of the reasons include -

  • Troubleshooting errors
  • To install third-party packages correctly
  • To create your known modules
  • To manage multiple Python environments
  • To understand Python's import system

Python's Module Search Path

When you import a module in Python, the interpreter searches for it in various locations following a specific order. This ordered list of directories is called the module search path.

Standard Library Module Locations

Python's standard library includes hundreds of modules that come pre-installed with Python. These modules are stored in -

On macOS and Linus -

/usr/lib/python<version>/
/usr/local/lib/python<version>/

On Windows -

C:\Python<version>\Lib\

Third-Party Module Location

Modules installed using package managers like pip are stored separately from the standard library. These third-party packages is stored in the "site-packages" directory -

On macOS and Linux -

/usr/local/lib/python<version>/site-packages/
~/.local/lib/python<version>/site-packages/ (for user installations)

On Windows -

C:\Python<version>\Lib\site-packages\
%APPDATA%\Python\Python<version>\site-packages\ (for user installations)

Virtual Environment Module Storage

Virtual Environments creates isolated Python installations, each with their own module directories -

venv_name/
??? bin/ (or Scripts/ on Windows)
??? include/
??? lib/
    ??? python<version>/
        ??? site-packages/

All modules installed while a virtual environment is active are stored in its site-packages directory.

Finding Module Locations

You can use the following Python code to find the location of modules -

Using sys.path

The code below is used to find the location of a module using the sys.path which is a built-in variable within the sys module -

import sys
print(sys.path)

Finding a Specific Module's Location

The code below demonstrates finding a specific module's location using module_path which is an engine-specific control that tells some engines where to find the module that they depend on -

import module_name
print(module_name.__file__)
Updated on: 2025-04-17T18:40:22+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started