Same folder

If the module is in your working directory, importlib.util.find_spec probably suffices for your purposes.

For example if you just want to load the module, you can use:

  • deprecated in Python 3.5 and higher:

    toolbox_specs = importlib.util.find_spec("Tools")
    toolbox = toolbox_specs.loader.load_module()
    
  • introduced in Python 3.5 and higher:

    toolbox_specs = importlib.util.find_spec("Tools")
    toolbox = importlib.util.module_from_spec(toolbox_specs)
    toolbox_specs.loader.exec_module(toolbox)
    

    Caveat: I haven’t tested this, but it’s straight from the documentation, so I suppose it works.

You can assess several other properties with the toolbox_specs object. However, e.g., a corresponding file object is not amongst them. If you really need this in Python 3, you probably have to obtain the file’s path and open it with other methods.

Different folder

To find a module in a different folder, you have to work with a FileFinder, which in turn needs to know the module’s type. For example, if your module is an extension, you can find the specs as follows:

loader_details = (
    importlib.machinery.ExtensionFileLoader,
    importlib.machinery.EXTENSION_SUFFIXES
    )

toolsfinder = importlib.machinery.FileFinder("Folder_of_Tools", loader_details)
toolbox_specs = toolsfinder.find_spec("Tools")

You can then process toolbox_specs as described above.

Answer from Wrzlprmft on Stack Overflow
Top answer
1 of 3
18

Same folder

If the module is in your working directory, importlib.util.find_spec probably suffices for your purposes.

For example if you just want to load the module, you can use:

  • deprecated in Python 3.5 and higher:

    toolbox_specs = importlib.util.find_spec("Tools")
    toolbox = toolbox_specs.loader.load_module()
    
  • introduced in Python 3.5 and higher:

    toolbox_specs = importlib.util.find_spec("Tools")
    toolbox = importlib.util.module_from_spec(toolbox_specs)
    toolbox_specs.loader.exec_module(toolbox)
    

    Caveat: I haven’t tested this, but it’s straight from the documentation, so I suppose it works.

You can assess several other properties with the toolbox_specs object. However, e.g., a corresponding file object is not amongst them. If you really need this in Python 3, you probably have to obtain the file’s path and open it with other methods.

Different folder

To find a module in a different folder, you have to work with a FileFinder, which in turn needs to know the module’s type. For example, if your module is an extension, you can find the specs as follows:

loader_details = (
    importlib.machinery.ExtensionFileLoader,
    importlib.machinery.EXTENSION_SUFFIXES
    )

toolsfinder = importlib.machinery.FileFinder("Folder_of_Tools", loader_details)
toolbox_specs = toolsfinder.find_spec("Tools")

You can then process toolbox_specs as described above.

2 of 3
15

I found this worked as a replacement for imp.find_module()

importlib.machinery.PathFinder().find_spec(mod_name, module_dir_list)
🌐
Python
docs.python.org › 3.9 › library › imp.html
imp — Access the import internals — Python 3.9.25 documentation
Load a module that was previously found by find_module() (or by an otherwise conducted search yielding compatible results). This function does more than importing the module: if the module was already imported, it will reload the module! The name argument indicates the full module name (including the package name, if this is a submodule of a package).
Discussions

Replace uses of imp.find_module / imp.load_module with importlib in the importer
As of 3.3, these functions are deprecated in favor of importlib implementations, which also happen to properly respect path hooks. This is interfering with a thing I'm writing, so if I get a mo... More on github.com
🌐 github.com
4
September 27, 2012
How do I find the location of Python module sources? - Stack Overflow
How do I learn where the source file for a given Python module is installed? Is the method different on Windows than on Linux? I'm trying to look for the source of the datetime module in particula... More on stackoverflow.com
🌐 stackoverflow.com
path - Python can't find my module - Stack Overflow
I have a python project (which I run within a virtualenv) and that has the following structure: Project ├───.git ├───venv └───src ├───__init__.py ├───mymodules │ ├───__init__.py ... More on stackoverflow.com
🌐 stackoverflow.com
How to find where my Python packages are going?
You can use pip show PACKAGE to show the location of an installed package in the current (virtual) environment. It may have been installed to a file/folder under a different name, and I don't think pip show will say that specifically, unfortunately. Edit adding the files flag (--files) may actually include the files you're looking for per the docs: https://pip.pypa.io/en/stable/cli/pip_show/ More on reddit.com
🌐 r/learnpython
9
8
November 18, 2022
🌐
Python documentation
docs.python.org › 3 › reference › import.html
5. The import system — Python 3.14.4 documentation
If the named module is not found in sys.modules, then Python’s import protocol is invoked to find and load the module. This protocol consists of two conceptual objects, finders and loaders. A finder’s job is to determine whether it can find the named module using whatever strategy it knows ...
🌐
Python Module of the Week
pymotw.com › 2 › imp
imp – Interface to module import mechanism. - Python Module of the Week
$ python imp_get_suffixes.py Extension Mode Type -------------------------------- .so rb extension module.so rb extension .py U source .pyc rb compiled · The first step to loading a module is finding it. find_module() scans the import search path looking for a package or module with the given name.
🌐
Python
docs.python.org › 3 › library › modulefinder.html
modulefinder — Find modules used by a script
This module provides a ModuleFinder class that can be used to determine the set of modules imported by a script. modulefinder.py can also be run as a script, giving the filename of a Python script as its argument, after which a report of the ...
Find elsewhere
🌐
GitHub
github.com › nose-devs › nose › issues › 559
Replace uses of imp.find_module / imp.load_module with importlib in the importer · Issue #559 · nose-devs/nose
September 27, 2012 - Replace uses of imp.find_module / imp.load_module with importlib in the importer#559 · Copy link · Julian · opened · on Sep 27, 2012 · Issue body actions · As of 3.3, these functions are deprecated in favor of importlib implementations, which also happen to properly respect path hooks.
Author   Julian
🌐
Cloudera Community
community.cloudera.com › t5 › Support-Questions › Correct-python-version-for-python-extension › m-p › 387962
Correct python version for python extension - Cloudera Community - 387962
July 10, 2024 - To resolve this issue, you'll need to update the code to use `find_spec` instead of `find_module`. Here's how you can modify the relevant part of your code: ```python Original code using find_module module = finder.find_module(name)
🌐
The Cloistered Monkey
necromuralist.github.io › posts › 201208python
Python's imp.find_module | The Cloistered Monkey
August 2, 2012 - imp.find_module(name [, path])ParametersnameThe name of the module (directory or file without extension).PathIf ommitted checks builtins first, then the sys.path directoriesIf given:must be a list of
🌐
Python
docs.python.org › 3 › library › importlib.html
importlib — The implementation of import
Finder for modules declared in the Windows registry. This class implements the importlib.abc.MetaPathFinder ABC. Only class methods are defined by this class to alleviate the need for instantiation. Added in version 3.3. Deprecated since version 3.6: Use site configuration instead. Future versions of Python may not enable this finder by default.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-locate-a-particular-module-in-python
How to locate a particular module in Python? - GeeksforGeeks
July 23, 2025 - In the python shell after we import some modules we can get various information about the module using help(module_name). For example, if we want to know the location of the module os using this method we will type the following in python shell ... Under various information, we will find a heading with the name FILE below which the location of the module will be present.
🌐
Leemendelowitz
leemendelowitz.github.io › blog › how-does-python-find-packages.html
How does python find packages? // Lee On Coding // My blog about coding and stuff.
Python exposes the entire import system through the imp module. That's pretty cool that all of this stuff is exposed for us to abuse, if we wanted to. ... > import imp > imp.find_module('numpy') (None, '/usr/local/lib/python2.7/dist-packages/numpy', ('', '', 5))
Top answer
1 of 5
74

Essentially, when you execute script.py directly, it doesn't know that it's part of a submodule of src, nor does it know where a module named src might be. This is the case in either python 2 or 3.

As you know, Python finds modules based on the contents of sys.path. In order to import any module, it must either be located in a directory that's listed in sys.path, or, in the same directory as the script that you're running.

When you say python src/scripts/script.py, sys.path includes the Project/src/scripts/ (because that's where script.py is located), but not Project. Because Project isn't in the path, the modules in that directory (src) aren't able to be imported.

To fix this:

I'm assuming that your script.py is an entry point for your src module (for example, maybe it's the main program). If that's true, then you could fix it by moving script.py up to the same level as src:

Project
├───.git
├───venv
|───script.py       <--- script.py moves up here
└───src
    ├───__init__.py
    └───mymodules
        ├───__init__.py
        ├───module1.py
        └───module2.py

This way, script.py can freely import anything in src, but nothing in src can import script.py.

If that's not the case, and script.py really is a part of src, you can use python's -m argument to execute script.py as part of the src module like so:

$ python -m src.scripts.script

Because you've told python which module you're running (src), it will be in the path. So, script.py will be aware that it's a submodule of src, and then will be able to import from src.

Be careful in this situation though - there's potential to create a circular import if something in src imports src.scripts.script.


As an alternative to both of these approaches, you can modify the sys.path directly in script.py:

import sys
sys.path.insert(0, '/path/to/Project') # location of src 

While this works, it's not usually my preference. It requires script.py to know exactly how your code is laid out, and may cause import confusion if another python program ever tries to import script.py.

2 of 5
7
Project
├───.git
├───venv
└───src
    ├───__init__.py
    ├───mymodules
    │   ├───__init__.py
    │   ├───module1.py
    │   └───module2.py
    └───scripts
        ├───__init__.py
        └───script.py

Alternatively you can import like the following in your script.py

import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__),'../../'))
import src.mymodules.module1

Now you can run script.py file from any location.

e.g :
python script.py
python /path to folder/script.py
🌐
Reddit
reddit.com › r/learnpython › how to find where my python packages are going?
r/learnpython on Reddit: How to find where my Python packages are going?
November 18, 2022 -

I did `pip list` and saw a list of packages. I wondered where they were coming from. I found a list of many of them in "/Users/myname/opt/anaconda3/pkgs". To check if that is where they go, I did `pip install python-math`. I did `pip list` again and saw python-math was in the list output in the terminal. I went to "/Users/myname/opt/anaconda3/pkgs" and python-math wasn't there. I did a search for "python-math" and couldn't find anything.

I would really like to have a logical understanding of where things go and what program puts them there and how to configure the program that puts them there. Any help is appreciated. I'm on a Mac M1 2020. I use VSCode. I have Xcode. Thank you.

🌐
Reddit
reddit.com › r/learnpython › python can't find module, even after pip install
r/learnpython on Reddit: Python can't find module, even after pip install
July 27, 2021 -

I'm trying to import/use an ETL package, PETL, with
import petl as etl

I did

pip install petl
from my project's root library; it appeared to install fine but VSCode is telling me it can't find the module; and when I try to run it from the terminal there it gives syntax error

I'm using python 3.7; I don't think I have multiple versions of python installed, but not sure how to check that
this seems to be a relatively common problem; a quick google answer says it's not looking where pip installed it
6 months ago I installed pymssql, am using that in the same script, and am able to find that fine (I'm just now getting back into a small python project)

🌐
AskPython
askpython.com › home › navigating python modules: 3 ways to find their locations
Navigating Python Modules: 3 Ways to Find their Locations - AskPython
May 12, 2023 - 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.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-modules
Python Modules | DigitalOcean
August 4, 2022 - Python package is a collection of python modules. Python module is a single python file whereas python package is a directory having multiple python scripts and __init__.py file defining the package details. You can find the list of Python modules from their official page of Python Module Index.
🌐
GitHub
bic-berkeley.github.io › psych-214-fall-2016 › sys_path.html
Where does Python look for modules? — Functional MRI methods
$ mkdir another_dir $ cd another_dir $ python3 ../scripts/a_script_with_hack.py Traceback (most recent call last): File "../scripts/a_script_with_hack.py", line 4, in <module> import a_module ModuleNotFoundError: No module named 'a_module' This is because the directory code that we specified is a relative path, and therefore Python looks for the code directory in the current working directory. To make the hack work when running the code from any directory, you could use some path manipulation on the The “__file__” variable: ... from os.path import dirname, abspath, join import sys # Find code directory relative to our directory THIS_DIR = dirname(__file__) CODE_DIR = abspath(join(THIS_DIR, '..', 'code')) sys.path.append(CODE_DIR) import a_module a_module.func()