import a_module
print(a_module.__file__)

Will actually give you the path to the .pyc file that was loaded, at least on Mac OS X. So I guess you can do:

import os
path = os.path.abspath(a_module.__file__)

You can also try:

path = os.path.dirname(a_module.__file__)

To get the module's directory.

Answer from orestis on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ sys_path_init.html
The initialization of the sys.path module search path โ€” Python 3.15.1 documentation
The first entry in the module search path is the directory that contains the input script, if there is one. Otherwise, the first entry is the current directory, which is the case when executing the interactive shell, a -c command, or -m module.
๐ŸŒ
GitHub
bic-berkeley.github.io โ€บ psych-214-fall-2016 โ€บ sys_path.html
Where does Python look for modules? โ€” Functional MRI methods
Python has a simple algorithm for finding a module with a given name, such as a_module. It looks for a file called a_module.py in the directories listed in the variable sys.path.
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ glossary โ€บ import-path
import path | Python Glossary โ€“ Real Python
In Python, the import path refers to the list of locations (path entries) that Pythonโ€™s path-based finder searches to find the modules you want to import.
๐ŸŒ
Codefinity
codefinity.com โ€บ blog โ€บ Understanding-Python-Module-Search-Path
Understanding Python Module Search Path
Python, known for its modular nature, allows the use of separate files and libraries to organize and reuse code efficiently. Central to this modularity is the Python module search path, a system that the Python interpreter uses to locate modules when an import statement is executed.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ examples โ€บ locate-python-modules
Navigating Python Modules: 3 Ways to Find their Locations - AskPython
May 12, 2023 - Why Do We Need To Find Module Sources And File Paths ยท In Python, there are three easy methods to find the location of module sources. The first is using the โ€˜fileโ€˜ attribute, which gives the absolute path of the current module. The second method involves the โ€˜helpโ€™ function, which provides comprehensive information about a module, including its location.
Find elsewhere
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python basics โ€บ python module search path
Python Module Search Path
October 15, 2020 - Python stores the resulting search path in the sys.path variable that comes from the sys module.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-locate-python-module-paths-418584
How to locate Python module paths | LabEx
Locating Python modules is an essential skill for developers to understand where modules are stored and how to access them. import sys ## Print all module search paths for path in sys.path: print(path)
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ examples โ€บ python-modules-path-retrieval
Python Moduleโ€™s Path Retrieval - AskPython
July 26, 2023 - This is a built-in attribute that throws the path from where the module was loaded. Typically works only with modules. A tool used to find the location of where the module exists in our system. Firstly for this example, I will be creating a module that is nothing but a Python file with a โ€˜.pyโ€™ extension.
๐ŸŒ
Nipraxis
textbook.nipraxis.org โ€บ sys_path.html
Where does Python look for modules? โ€” Practice and theory of brain imaging
Python has a simple algorithm for finding a module with a given name, such as a_module. It looks for a file called a_module.py in the directories listed in the variable sys.path.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-locate-a-particular-module-in-python
How to locate a particular module in Python? - GeeksforGeeks
July 23, 2025 - For a pure Python module, we can locate its source by module_name.__file__. This will return the location where the module's .py file exists. To get the directory we can use os.path.dirname() method in OS module.
๐ŸŒ
Linuxtopia
linuxtopia.org โ€บ online_books โ€บ programming_books โ€บ python_programming โ€บ python_ch28s04.html
Python - Finding Modules: The Path
For modules to be available for use, the Python interpreter must be able to locate the module file. Python has a set of directories in which it looks for module files. This set of directories is called the search path, and is analogous to the PATH environment variable used by an operating system ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ modules.html
6. Modules โ€” Python 3.15.1 documentation
When importing the package, Python searches through the directories on sys.path looking for the package subdirectory. The __init__.py files are required to make Python treat directories containing the file as packages (unless using a namespace package, a relatively advanced feature). This prevents directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.
๐ŸŒ
Real Python
realpython.com โ€บ videos โ€บ module-search-path
The Module Search Path (Video) โ€“ Real Python
Or put mod.py in one of the directories already contained in the PYTHONPATH variable. Put mod.py in one of the installation-dependent directories, which you may or may not have write-access to, depending on the OS. There is also one additional option: You can put the module file in any directory of your choice and then modify sys.path at run-time so that it contains that directory.
Published: January 28, 2020
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-retrieve-python-module-path
How to retrieve Python module path?
March 24, 2026 - Use module.__file__ for quick path retrieval or inspect.getfile() for more programmatic approaches. Both methods are reliable for finding where Python modules are located on your system.
Top answer
1 of 3
171

How python finds its modules

Strictly taken, a module is a single python file, while a package is a folder containing python files, accompanied by a (can be empty) file named __init__.py, to tell python it is a package to import modules from. In both cases, modules need their .py extension, but importing them is done without (see further below).

By default, Python looks for its modules and packages in $PYTHONPATH.

To find out what is included in $PYTHONPATH, run the following code in python (3):

import sys
print(sys.path)

How to add a directory

Occasionally

From within a python file, you can add path(s) occasionally to the default path by adding the following lines in the head section of your python application or script:

import sys
sys.path.insert(0, "/path/to/your/package_or_module")

For example:

if I have a folder: /home/myname/pythonfiles, and I want to import the file module_1.py, located in that directory, I add this to the head section of my code:

import sys
sys.path.insert(0, "/home/myname/pythonfiles")

And I can simply import the file module_1.py by:

import module_1

When I create a package and want to import module(s) from the package, I need to create a folder in $PYTHONPATH, containing the modules, accompanied by a (can be empty) file called __init__.py

For example:

To import from a package (folder) called my_package in /home/myname/pythonfiles , add the /home/myname/pythonfiles path to your $PYTHONPATH, like in example 1, and import the module called module_2.py (inside the package folder) simply with: `

from <packagename> import module_2

Adding directories to $PYTHONPATH permanently:

Add the following line to your ~/.profile file.

export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add

Subdirectories

From within a package, subdirectories are not included just like that; you need to "chain" the directories. To import a module module_3.py, inside folder subfolder inside folder packagename:

import packagename.subfolder.module_3

Given the fact that all subfolders in the package include their own __init__.py file.

When a module is in the same directory as the script or application

There is no need to insert the path to a module when it is in the same directory as the script or application, it is automatically added.

Example:

If I have a folder, containing script.py and module.py, I can simply import the module by:

import module
2 of 3
7

The correct way to use relative import is:

from ..Common import foo1

And you also need a __init__.py in all your folders.

๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Check and Add the Module Search Path in Python: sys.path | note.nkmk.me
May 12, 2023 - In Python, the module search path is a list of directories that are searched when importing modules and packages using import. This search path is stored in sys.path. 6. Modules - The Module Search Pa ...
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Where to put the module? - Python Help - Discussions on Python.org
January 17, 2024 - I downloaded a package from Github. In some directory A, there is a jupyter file which starts with from some_module import some_function I find that the file โ€˜some_module.pyโ€™ is not in directory A, but in a sibling directory of A. How did vscode or jupyter know where to look for the module ...