TL;DR: You can't do relative imports from the file you execute since __main__ module is not a part of a package.

Absolute imports - import something available on sys.path

Relative imports - import something relative to the current module, must be a part of a package

If you're running both variants in exactly the same way, one of them should work. Here is an example that should help you understand what's going on. Let's add another main.py file with the overall directory structure like this:

.
./main.py
./ryan/__init__.py
./ryan/config.py
./ryan/test.py

And let's update test.py to see what's going on:

# config.py
debug = True
# test.py
print(__name__)

try:
    # Trying to find module in the parent package
    from . import config
    print(config.debug)
    del config
except ImportError:
    print('Relative import failed')

try:
    # Trying to find module on sys.path
    import config
    print(config.debug)
except ModuleNotFoundError:
    print('Absolute import failed')
# main.py
import ryan.test

Let's run test.py first:

$ python ryan/test.py
__main__
Relative import failed
True

Here "test" is the __main__ module and doesn't know anything about belonging to a package. However import config should work, since the ryan folder will be added to sys.path.

Let's run main.py instead:

$ python main.py
ryan.test
True
Absolute import failed

And here test is inside of the "ryan" package and can perform relative imports. import config fails since implicit relative imports are not allowed in Python 3.

Hope this helped.

P.S.: If you're sticking with Python 3 there is no more need for __init__.py files.

Answer from Igonato on Stack Overflow
🌐
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 - Now we're up to speed with how the basic import statement is executed and what is the difference between absolute and relative imports we can now go ahead and discuss what to do when your Python application fails with either ModuleNotFoundError ** or** ImportError . In most of the cases, either of the errors occur due to the fact that Python is unable to resolve the module's name in sys.path .
Top answer
1 of 16
354

TL;DR: You can't do relative imports from the file you execute since __main__ module is not a part of a package.

Absolute imports - import something available on sys.path

Relative imports - import something relative to the current module, must be a part of a package

If you're running both variants in exactly the same way, one of them should work. Here is an example that should help you understand what's going on. Let's add another main.py file with the overall directory structure like this:

.
./main.py
./ryan/__init__.py
./ryan/config.py
./ryan/test.py

And let's update test.py to see what's going on:

# config.py
debug = True
# test.py
print(__name__)

try:
    # Trying to find module in the parent package
    from . import config
    print(config.debug)
    del config
except ImportError:
    print('Relative import failed')

try:
    # Trying to find module on sys.path
    import config
    print(config.debug)
except ModuleNotFoundError:
    print('Absolute import failed')
# main.py
import ryan.test

Let's run test.py first:

$ python ryan/test.py
__main__
Relative import failed
True

Here "test" is the __main__ module and doesn't know anything about belonging to a package. However import config should work, since the ryan folder will be added to sys.path.

Let's run main.py instead:

$ python main.py
ryan.test
True
Absolute import failed

And here test is inside of the "ryan" package and can perform relative imports. import config fails since implicit relative imports are not allowed in Python 3.

Hope this helped.

P.S.: If you're sticking with Python 3 there is no more need for __init__.py files.

2 of 16
177

You have to append your project's path to PYTHONPATH and make sure to use absolute imports.


For UNIX (Linux, OSX, ...)

export PYTHONPATH="${PYTHONPATH}:/path/to/your/project/"

For Windows

set PYTHONPATH=%PYTHONPATH%;C:\path\to\your\project\

Absolute imports

Assuming that we have the following project structure,

└── myproject
    β”œβ”€β”€ mypackage
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   β”œβ”€β”€ a.py
    └── anotherpackage
        β”œβ”€β”€ __init__.py
        β”œβ”€β”€ b.py
        β”œβ”€β”€ c.py
        └── mysubpackage
            β”œβ”€β”€ __init__.py
            └── d.py

just make sure to reference each import starting from the project's root directory. For instance,

# in module a.py
import anotherpackage.mysubpackage.d

# in module b
import anotherpackage.c
import mypackage.a

For a more comprehensive explanation, refer to the article How to fix ModuleNotFoundError and ImportError

Discussions

python - ModuleNotFoundError: No module named - Stack Overflow
Python Unit test module throws "ModuleNotFoundError: No module named 'tests.test_file'" ... Your problem might stem from the fact that the directories which you call modules are in fact, in Python's eyes so to speak, nothing but mere directories. In order to get them to be perceived by the interpreter as modules you will have to include ... More on stackoverflow.com
🌐 stackoverflow.com
Python error "ImportError: No module named" - Stack Overflow
Python is installed in a local directory. My directory tree looks like this: (local directory)/site-packages/toolkit/interface.py My code is in here: (local directory)/site-packages/toolkit/examples/ More on stackoverflow.com
🌐 stackoverflow.com
How to solve ModuleNotFoundError: No module named in Python?
Neither of these files can import the other, because when you run one of them, the other won’t be on sys.path. You can’t bury your project’s entrypoints like this and I’m starting to wonder what guidance is out there that’s telling people to do this. More on reddit.com
🌐 r/learnpython
16
3
November 18, 2022
Why do I get a ModuleNotFoundError?
You probably have a circular dependency - abc imports xyz and xyz imports abc. Python can't complete either import so it raises an error. More on reddit.com
🌐 r/learnpython
4
1
September 2, 2021
🌐
PyTutorial
pytutorial.com β€Ί how-to-solve-modulenotfounderror-no-module-named-in-python
How To Solve ModuleNotFoundError: No module named in Python …
August 11, 2026 - Solving ModuleNotFoundError is straightforward. Start with installing the module using pip. Then, check your environment and virtual environment. Verify the module name and fix the Python path if needed.
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί module-not-found-error-in-python-solved
ModuleNotFoundError: no module named Python Error [Fixed]
September 12, 2022 - Sometimes, Python throws the ModuleNotFoundError afterward. What does this error mean in Python? As the name implies, this error occurs when you're trying to access or use a module that cannot be found.
🌐
Medium
medium.com β€Ί @ryan_forrester_ β€Ί fixing-no-module-named-errors-in-python-a-complete-guide-b989b7a2bd19
Fixing β€˜No Module Named’ Errors in Python: A Complete Guide | by ryan | Medium
May 14, 2025 - The dreaded β€œNo module named” error is something every Python developer encounters. Let’s break down why this happens and how to fix it across different scenarios. When Python can’t find a module you’re trying to import, it throws a `ModuleNotFoundError`. Here’s what it looks like:
🌐
OneUptime
oneuptime.com β€Ί home β€Ί blog β€Ί how to fix 'importerror: no module named' in python
How to Fix 'ImportError: No module named' in Python
January 26, 2026 - This guide covers common causes including virtual environments, path issues, and package installation problems. By @nawazdhandala β€’ Jan 26, 2026 β€’ Reading time Β· Python ImportError Debugging Module Virtual Environments Package Management Β· The "ImportError: No module named X" error is one of the most common issues Python developers encounter. In Python 3, this is usually reported as ModuleNotFoundError: No module named 'X', which is a subclass of ImportError.
🌐
CodeRivers
coderivers.org β€Ί blog β€Ί python-modulenotfounderror-no-module-named
Python ModuleNotFoundError: No module named - CodeRivers
August 8, 2026 - Python is a powerful and versatile ... developers encounter is the ModuleNotFoundError: No module named. This error occurs when Python cannot locate a specific module that your code is trying to import....
🌐
Medium
alinawrites.medium.com β€Ί how-to-fix-modulenotfounderror-no-module-named-error-in-python-2d66875df96
How to Fix ModuleNotFoundError (No Module Named) Error in Python | by Alina Writes | Medium
August 3, 2022 - If you get the error ModuleNotFoundError: No module named you need to install the missing module. ... Be sure to replace module_name with actual name of the module you’re trying to install.
Find elsewhere
🌐
Curiousefficiency
python-notes.curiousefficiency.org β€Ί en β€Ί latest β€Ί python_concepts β€Ί import_traps.html
Traps for the Unwary in Python’s Import System - Alyssa Coghlan's Python Notes
This particular trap applies to 2.x releases, as well as 3.x releases up to and including 3.2. Prior to Python 3.3, filesystem directories, and directories within zipfiles, had to contain an __init__.py in order to be recognised as Python package directories. Even if there is no initialisation code to run when the package is imported, an empty __init__.py file is still needed for the interpreter to find any modules or subpackages in that directory. This has changed in Python 3.3: now any directory on sys.path with a name that matches the package name being looked for will be recognised as contributing modules and subpackages to that package.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί modulenotfounderror-no-module-named-error-in-python
ModuleNotFoundError: No module named Error in Python - GeeksforGeeks
July 23, 2025 - When Python encounters an import statement, it searches for the specified module in its standard library and other directories. If the module is not found, the "No Module Named..." error is triggered. Below, are the reasons for occurring ModuleNotFoundError: No module named Error in Python:
🌐
OneUptime
oneuptime.com β€Ί home β€Ί blog β€Ί how to fix 'modulenotfounderror' in python
How to Fix 'ModuleNotFoundError' in Python
January 24, 2026 - The ModuleNotFoundError (introduced in Python 3.6 as a subclass of ImportError) occurs when Python cannot find the module you are trying to import. Understanding why this happens requires knowing how Python's import system works.
🌐
PyDevTools
pydevtools.com β€Ί home β€Ί python developer tooling handbook β€Ί how to β€Ί how to fix modulenotfounderror: no module named 'package'
Fix ModuleNotFoundError: No Module Named (Any Package) | pydevtools
September 7, 2026 - $ uv run python run.py Traceback (most recent call last): File "/tmp/srcproj/run.py", line 1, in <module> from myapp import hello ModuleNotFoundError: No module named 'myapp' A project without a [build-system] table is a bare collection of dependencies, so uv never installs the project’s own code. Declare a build backendThe tool that does the actual work of turning your source code into an installable package. Examples include hatchling, setuptools, and uv_build.
🌐
Itsourcecode
itsourcecode.com β€Ί home β€Ί modulenotfounderror: no module named, 8 fixes (2026)
ModuleNotFoundError: No Module Named, 8 Fixes (2026)
July 12, 2026 - Fix Python ModuleNotFoundError: No module named in minutes. 8 real causes (pip, venv, interpreter, uv, conda) with code examples. Updated 2026 guide.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί how-to-fix-the-module-not-found-error
How to Fix The Module Not Found Error? - GeeksforGeeks
April 28, 2025 - Check if the module name and spelling are correct or not ... Whenever you encounter "ModuleNotFoundError" can check if the imported modules are installed in your Python environment by using 'try' and 'except' blocks to handle the error gracefully.
Top answer
1 of 16
327

Based on your comments to orip's answer, I guess this is what happened:

  1. You edited __init__.py on Windows.
  2. The Windows editor added something nonprinting, perhaps a carriage-return (end-of-line in Windows is CR/LF; in Unix it is LF only), or perhaps a Ctrl + Z (windows end-of-file).
  3. You used WinSCP to copy the file to your Unix box.
  4. WinSCP thought: "This has something that's not basic text; I'll put a .bin extension to indicate binary data."
  5. The missing __init__.py file (now called __init__.py.bin) means Python doesn't understand toolkit as a package.
  6. You create the __init__.py file in the appropriate directory and everything works...?
2 of 16
116

I ran into something very similar when I did this exercise in LPTHW; I could never get Python to recognise that I had files in the directory I was calling from. But I was able to get it to work in the end. What I did, and what I recommend, is to try this:

(NOTE: From your initial post, I am assuming you are using an *NIX-based machine and are running things from the command line, so this advice is tailored to that. Since I run Ubuntu, this is what I did)

  1. Change directory (cd) to the directory above the directory where your files are. In this case, you're trying to run the mountain.py file, and trying to call the toolkit.interface.py module, which are in separate directories. In this case, you would go to the directory that contains paths to both those files (or in other words, the closest directory that the paths of both those files share). Which in this case is the toolkit directory.

  2. When you are in the toolkit directory, enter this line of code on your command line:

    export PYTHONPATH=.

    This sets your PYTHONPATH to ".", which basically means that your PYTHONPATH will now look for any called files within the directory you are currently in, (and more to the point, in the sub-directory branches of the directory you are in. So it doesn't just look in your current directory, but in all the directories that are in your current directory).

  3. After you've set your PYTHONPATH in the step above, run your module from your current directory (the toolkit directory). Python should now find and load the modules you specified.

🌐
CodeWithHarry
codewithharry.com β€Ί blogpost β€Ί solved-modulenotfounderror-no-module-named-in-python
[Solved] ModuleNotFoundError: no module named ' ' in Python | Blog | CodeWithHarry
Incorrect Module Name: Now this will sound very naive but make sure you have typed the correct module name. Python is a case-sensitive programming language, which means that it distinguishes between lowercase and uppercase letters. Even a single letter out of place can lead to the "ModuleNotFoundError".
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί how to solve modulenotfounderror: no module named in python?
r/learnpython on Reddit: How to solve ModuleNotFoundError: No module named in Python?
November 18, 2022 -

I've got two scripts in my directory. Normally I would be able to import modules from one to another, but I'm suddenly getting this error:

ModuleNotFoundError: No module named modulename in Python

I've searched and tried solutions, but I don't think I properly understand how to solve it.

I'm not sure what's happened. When I check my working directory, os.getcwd() it spits out the correct one. All modules are in the same folder, and I used to be able to import them, until I added a new .py file. My original modules still work, even though they get the same error when i do:

import modulename

I can't tell if there's some structure I've missed, nor do I know how to check it, something like this

└── test
    β”œβ”€β”€ demoA
        β”œβ”€β”€ __init__.py
    β”‚   β”œβ”€β”€ test1.py
    └── demoB
        β”œβ”€β”€ __init__.py
        β”œβ”€β”€ test2.py

I should mention that the original directory (call it A) is forked, I haven't made or changed anything in it. So I must have messed something up. Should I delete my file and put it in a different directory (call it B)? If so, how do I call a specific .py file from A in my new B? It's my understanding that it needs to be in the same one, but I'm completely new to this, and basically know nothing. I just know that import module name should normally work :(

🌐
Streamlit
docs.streamlit.io β€Ί knowledge-base β€Ί dependencies β€Ί module-not-found-error
ModuleNotFoundError No module named - Streamlit Docs
You receive the error ModuleNotFoundError: No module named when you deploy an app on Streamlit Community Cloud. This error occurs when you import a module on Streamlit Community Cloud that isn’t included in your requirements file.
🌐
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