This is a very typical issue, so I will leave this here for future reference.

If I have a project called: my_project

.
└── my_project
    β”œβ”€β”€ first_folder
    β”œβ”€β”€ second_folder
    └── third_folder

What you want to do is one of these two things:

  1. PYTHONPATH (only Python)

cd ~/my_project && export PYTHONPATH=$(pwd)

change dir to root dir, and export that dir to PYTHONPATH, so when Python runs it looks in the PYTHONPATH.

  1. PATH (everything)

cd ~/my_project && export PATH=$PATH:$(pwd)

Same as above!

This needed to live somewhere since it took me a while to figure out. So for anyone in the future who needs help with this!

Answer from DUDANF on Stack Overflow
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί module-not-found-error-in-python-solved
ModuleNotFoundError: no module named Python Error [Fixed]
September 12, 2022 - In it, you have two folders demoA and demoB. demoA has an __init__.py file (to show it's a Python package) and a test1.py module. demoA also has an __init__.py file and a test2.py module. ... └── test β”œβ”€β”€ demoA β”œβ”€β”€ __init__.py β”‚ β”œβ”€β”€ test1.py └── demoB β”œβ”€β”€ __init__.py β”œβ”€β”€ test2.py ... And let's say you want to use this declared hello function in test2.py. The following code will throw a module not found error:
Discussions

Python: No module found even though it exists in directory - Stack Overflow
So you'll either have to add the ... source folder of your project to a .pth file in your site-packages as shown here ... I receive error when trying the relative import E0402: Attempted relative import beyond top-level package. 2018-07-13T11:10:48.527Z+00:00 ... Community Asks Sprint Announcement – January 2026: Custom site-specific badges! Stack Overflow chat opening ... More on stackoverflow.com
🌐 stackoverflow.com
July 13, 2018
Improve ModuleNotFoundError to cater for condition when directory exists
there are conditions when the directory exists eg. library but python complains - ModuleNotFoundError: No module named library the library directory has the necessary init.py file - but because the code was not invoked with python -m module - then it fails to find them. from secuity perspective ... More on discuss.python.org
🌐 discuss.python.org
3
0
April 18, 2024
Python doesn't find local module although it is installed and its location exists in sys.path
I installed a local module using the pyproject.toml approach by following the great tutorial by RealPython: Packaging Your Python Code With pyproject.toml | Complete Code Conversation - YouTube Although the module seems to be locally installed, I am unable to invoke it from outside the project ... More on discuss.python.org
🌐 discuss.python.org
6
0
March 23, 2023
python - 'ModuleNotFoundError' when trying to import module from imported package - Stack Overflow
This is my directory structure: man/ Mans/ man1.py MansTest/ SoftLib/ Soft/ SoftWork/ ... More on stackoverflow.com
🌐 stackoverflow.com
December 7, 2021
🌐
Containersolutions
containersolutions.github.io β€Ί runbooks β€Ί posts β€Ί python β€Ί module-not-found
Module Not Found – Runbooks
The sys.path value is figured out on startup by Python running a site.py located under the Python installation. This dynamically picks up relevant folders based on the code in that file. After you have determined that the module exists, check that the module loads when you run Python on the command line.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί how-to-fix-the-module-not-found-error
How to Fix The Module Not Found Error? - GeeksforGeeks
April 28, 2025 - This error occurs when you have not installed that module on your system and try to access the module that the Python interpreter cannot locate in its search path or in the Python path, If python cannot find the module you're trying to import ...
🌐
Towards Data Science
towardsdatascience.com β€Ί home β€Ί programming β€Ί how to fix modulenotfounderror and importerror
How to Fix ModuleNotFoundError and ImportError | Towards Data Science
September 13, 2020 - A python module is a single file with a .py extension. A python package is a folder that contains at least one python module.
Find elsewhere
🌐
Python.org
discuss.python.org β€Ί python help
Python doesn't find local module although it is installed and its location exists in sys.path - Python Help - Discussions on Python.org
March 23, 2023 - I installed a local module using the pyproject.toml approach by following the great tutorial by RealPython: Packaging Your Python Code With pyproject.toml | Complete Code Conversation - YouTube Although the module seems to be locally installed, I am unable to invoke it from outside the project directory.
Top answer
1 of 6
90

FIRST, if you want to be able to access man1.py from man1test.py AND manModules.py from man1.py, you need to properly setup your files as packages and modules.

Packages are a way of structuring Python’s module namespace by using β€œdotted module names”. For example, the module name A.B designates a submodule named B in a package named A.

...

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 the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.

You need to set it up to something like this:

man
|- __init__.py
|- Mans
   |- __init__.py
   |- man1.py
|- MansTest
   |- __init__.py
   |- SoftLib
      |- Soft
         |- __init__.py
         |- SoftWork
            |- __init__.py
            |- manModules.py
      |- Unittests
         |- __init__.py
         |- man1test.py

SECOND, for the "ModuleNotFoundError: No module named 'Soft'" error caused by from ...Mans import man1 in man1test.py, the documented solution to that is to add man1.py to sys.path since Mans is outside the MansTest package. See The Module Search Path from the Python documentation. But if you don't want to modify sys.path directly, you can also modify PYTHONPATH:

sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.

THIRD, for from ...MansTest.SoftLib import Soft which you said "was to facilitate the aforementioned import statement in man1.py", that's now how imports work. If you want to import Soft.SoftLib in man1.py, you have to setup man1.py to find Soft.SoftLib and import it there directly.

With that said, here's how I got it to work.

man1.py:

from Soft.SoftWork.manModules import *
# no change to import statement but need to add Soft to PYTHONPATH

def foo():
    print("called foo in man1.py")
    print("foo call module1 from manModules: " + module1())

man1test.py

# no need for "from ...MansTest.SoftLib import Soft" to facilitate importing..
from ...Mans import man1

man1.foo()

manModules.py

def module1():
    return "module1 in manModules"

Terminal output:

$ python3 -m man.MansTest.Unittests.man1test
Traceback (most recent call last):
  ...
    from ...Mans import man1
  File "/temp/man/Mans/man1.py", line 2, in <module>
    from Soft.SoftWork.manModules import *
ModuleNotFoundError: No module named 'Soft'

$ PYTHONPATH=$PYTHONPATH:/temp/man/MansTest/SoftLib
$ export PYTHONPATH
$ echo $PYTHONPATH
:/temp/man/MansTest/SoftLib
$ python3 -m man.MansTest.Unittests.man1test
called foo in man1.py
foo called module1 from manModules: module1 in manModules 

As a suggestion, maybe re-think the purpose of those SoftLib files. Is it some sort of "bridge" between man1.py and man1test.py? The way your files are setup right now, I don't think it's going to work as you expect it to be. Also, it's a bit confusing for the code-under-test (man1.py) to be importing stuff from under the test folder (MansTest).

2 of 6
9

I had a similar problem, although not the same.

My folders and files had the structure (GUI_ML is the main folder):

GUI_ML
\ Views \ login.py
\ Views \ __ init __.py
\ Controllers \ Control_login.py
\ Controllers \ __ init __.py

I needed to import Control_login.py from login.py. The following code solved my problem:

import sys
import os
myDir = os.getcwd()
sys.path.append(myDir)

from pathlib import Path
path = Path(myDir)
a=str(path.parent.absolute())

sys.path.append(a)

from Controllers.Control_login import Control_login
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί why do i get a modulenotfounderror?
r/learnpython on Reddit: Why do I get a ModuleNotFoundError?
September 2, 2021 -

Hey Guys,

I get a weird ModuleNotFoundError in one of my projects and i do not really understand why. So here is my Folder Structure:

Project\

| main.py

| packageabc\

| | __init__.py

| | abc.py

| | packagexyz\

| | | xyz.py

In every .py file is an abc/xyz or main class. Now the interesting part. Inside the abc.py I do an import like this:

from packagexyz.xyz import xyz

This works perfectly fine!

Inside the main.py I do an import like this:

from packageabc.abcimport abc

Now I get an ModuleNotFoundError. Error Code looks like this:

from packagexyz.xyz import xyz ModuleNotFoundError: no module named packagexyz.

So the import in the abc.py File that works perfectly when tested inside the file does not work if the abc.py file is imported. So why is that?

Top answer
1 of 2
2
You probably have a circular dependency - abc imports xyz and xyz imports abc. Python can't complete either import so it raises an error.
2 of 2
1
It's because of the way Python looks for modules. There's a list of paths that Python looks through to find modules, which automatically includes things like the place pip installs packages to. It also automatically includes the directory the executing script is in. It does not, however, automatically include the directory an imported module is in. So, if you're executing "abc.py" directly, in finds "packagexyz" just fine, since it knows to look in the "packageabc" folder for modules. If you are executing "main.py" instead, the only place it looks (besides the default and manually configured paths) is "Project", so it can't find packagexyz. There's really three ways you could fix this: Manually configuring paths You can keep the imports as you have them and simply ensure that the "packageabc" folder is always on the search path for modules. You can do so either by configuring the PYTHONPATH environment variable to include it or by appending it to sys.path in your "main.py" before importing abc. Absolute imports You could also change the import in "abc.py" to be relative to "Project" (from packageabc.packagexyz.xyz import xyz). Then your "main.py" would work the way it is. Of course, that in turn means you can no longer directly execute "abc.py" unless you manually configure "Project" to always be on the search path for modules. Relative imports You could also change "abc.py" to use relative imports (from .packagexyz.xyz import xyz, note the extra dot in front). Then your "main.py" would work the way it is, as well. You could also still directly execute "abc.py", but only if you make sure to do it in a way that Python is aware it is part of a larger package (otherwise, relative imports give you an error). Specifically, you'd always need to execute it like this: python -m packageabc.abc Rather than like this: python packageabc/abc.py
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί how-to-fix-the-module-not-found-error
How to Fix The Module Not Found Error - Python
July 1, 2026 - Traceback (most recent call last): ... or in another virtual environment, but not in the currently active one. 1. Check Whether the Module is Installed: You can use a try-except block to verify whether a module exists...
🌐
Python.org
discuss.python.org β€Ί python help
ModuleNotFoundError: No module named 'Directory_one' - Python Help - Discussions on Python.org
August 26, 2021 - Hi All, I am trying to access method from another module in one of my file but I keep getting ModuleNotFoundError. This how my folder structure is: Appreciate for your help Thanks Lax
🌐
Prakashinfotech
prakashinfotech.com β€Ί q-and-a-module-not-found-error
What does the "Module Not Found Error" in Python indicate, and how can it be resolved?
October 27, 2025 - The "ModuleNotFoundError" in Python occurs when the interpreter cannot find the specified module in your code.