How do I find the location of Python module sources?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
To find where a Python module's source code is located on disk, use module.__file__, inspect.getfile(module), or run python -m site to see all module search paths. This is useful for debugging import issues, reading library source code, finding which version of a module is actually imported, and locating configuration files installed alongside packages.
Method 1: module.file
The __file__ attribute contains the path to the module's source file:
For packages (directories with __init__.py), __file__ points to __init__.py. The parent directory is the package root.
Handling Missing file
Some built-in modules (written in C) do not have __file__:
Check first:
Method 2: inspect.getfile()
The inspect module provides a more robust way:
inspect.getfile() raises TypeError for built-in modules:
Method 3: From the Command Line
pip show
python -c
python -m site
Method 4: sys.path and Import Search Order
Python searches for modules in directories listed in sys.path:
Python checks these paths in order. The first match wins:
Method 5: importlib
Finding the Source Directory
Virtual Environment Module Locations
Common Pitfalls
- Shadowing standard library modules with local files: A file named
json.pyorrandom.pyin your project directory shadows the standard library module. Python searches the current directory first, so your local file gets imported instead. - Built-in modules have no
__file__: Modules likesys,builtins, and sometimesmathare implemented in C and compiled into the interpreter. They have no file path. Checkhasattr(module, '__file__')before accessing it. - Confusing
.pycwith.pypaths:__file__may point to a.pyc(compiled bytecode) file instead of the.pysource. Useimportlib.util.find_spec()which reports the source file, or check if a.pyfile exists at the same path. - Different module paths in virtual environments: A module may be installed globally and in a virtual environment. The path depends on which Python interpreter is active. Always verify with
python -c "import x; print(x.__file__)"from the intended environment. - Editable installs (
pip install -e) point to the source directory: Editable installs symlink to the development directory. The module path reflects the source location, notsite-packages, which can be confusing when debugging import issues.
Summary
- Use
module.__file__for the quickest way to find a module's source file - Use
inspect.getfile(module)for a more robust alternative that also works on classes and functions - Use
pip show package_namefrom the command line to find package location and metadata - Use
importlib.util.find_spec()to find a module path without importing it - Check
sys.pathto understand the import search order and debug shadowing issues
Related reading
- How do I find the time difference between two datetime objects in python?
- How do I find Wally with Python?
- How do I find which attributes my tree splits on, when using scikit-learn?
- How do I fix 'ImportError cannot import name IncompleteRead'?
- How do I find the stack trace in Visual Studio?
- How do I find which transaction is causing a Waiting for table metadata lock state?
- How do I format a date in Jinja2?
- How do I format a string using a dictionary in python-3.x?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.