In your particular case it looks like you're trying to import SomeObject from the myapp.py and TestCase.py scripts. From myapp.py, do

import SomeObject

since it is in the same folder. For TestCase.py, do

from ..myapp import SomeObject

However, this will work only if you are importing TestCase from the package. If you want to directly run python TestCase.py, you would have to mess with your path. This can be done within Python:

import sys
sys.path.append("..")
from myapp import SomeObject

though that is generally not recommended.

In general, if you want other people to use your Python package, you should use distutils to create a setup script. That way, anyone can install your package easily using a command like python setup.py install and it will be available everywhere on their machine. If you're serious about the package, you could even add it to the Python Package Index, PyPI.

Answer from David Robinson on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › reference › import.html
5. The import system — Python 3.15.2 documentation
Finders do not actually load modules. If they can find the named module, they return a module spec, an encapsulation of the module’s import-related information, which the import machinery then uses when loading the module.
Discussions

Can't import modules
I'm willing to bet that this is caused by your IDE using a virtual environment without you realising it, and those packages are installed globally. Are you using PyCharm or Thonny by any chance? More on reddit.com
🌐 r/learnpython
20
39
December 22, 2022
Python Module Importing Problems
I’ve looked all over the Internet and gone to tech support a couple times with no luck on solving this issue. Some modules are automatically added with a python download (such as math and random) which I can access without an issue. However, although my command prompts shows manually added ... More on discuss.python.org
🌐 discuss.python.org
8
0
March 26, 2024
[SOLVED] Module installed but cannot import
Hi everyone, I’m extremely new to all of this. I went and installed Rich via pip and added the line from rich import pretty but I’m getting the error 'no module named ‘rich’. My module is located in \venv\Lib\site-packages\pip_vendor\rich\pretty.py I also ran pip --version and this ... More on discuss.python.org
🌐 discuss.python.org
2
0
September 20, 2023
Python can't find module in the same folder - Stack Overflow
My python somehow can't find any modules in the same directory. What am I doing wrong? (python2.7) So I have one directory '2014_07_13_test', with two files in it: ... >>> Traceback (most recent call last): File "", line 1, in File "", line 4, in ImportError: No ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-fix-the-module-not-found-error
How to Fix The Module Not Found Error - Python
July 1, 2026 - A ModuleNotFoundError, occurs when Python cannot locate the module you are trying to import. This usually happens when the module is not installed, the module name is incorrect, or Python cannot find it in its search path.
🌐
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
If we explicitly add the second project to PYTHONPATH, we find that Python 3.3+ can’t find example.foo either, as the directory containing __init__.py file prevents the creation of a multi-directory namespace package: $ PYTHONPATH=../project2 python3 -c "import example.bar" Hello from example.bar $ PYTHONPATH=../project2 python3 -c "import example.foo" Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: No module named 'example.foo'
🌐
freeCodeCamp
freecodecamp.org › news › module-not-found-error-in-python-solved
ModuleNotFoundError: no module named Python Error [Fixed]
September 12, 2022 - The reason for this is that we have used the wrong path to access the test1 module. The right path should be demoA.test1. When you correct that, the code works: ... For resolving an imported module, Python checks places like the inbuilt library, installed modules, and modules in the current project.
🌐
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 - Recall that when you call import a if the module's name was found neither in sys.modules nor in standard library, Python will try to resolve it in sys.path . Likewise, when you use from syntax (e.g. from mypackage import a ), Python will first attempt to find and load the module.
🌐
Python.org
discuss.python.org › python help
Python Module Importing Problems - Python Help - Discussions on Python.org
March 26, 2024 - I’ve looked all over the Internet and gone to tech support a couple times with no luck on solving this issue. Some modules are automatically added with a python download (such as math and random) which I can access without an issue. However, although my command prompts shows manually added modules such as numpy, networkx, django, etc. have been installed, Python doesn’t recognize them.
Find elsewhere
🌐
Python.org
discuss.python.org › python help
[SOLVED] Module installed but cannot import - Python Help - Discussions on Python.org
September 20, 2023 - Hi everyone, I’m extremely new to all of this. I went and installed Rich via pip and added the line from rich import pretty but I’m getting the error 'no module named ‘rich’. My module is located in \venv\Lib\site-packages\pip_vendor\rich\pretty.py I also ran pip --version and this is my output: pip 23.2.1 from C:\Users \AppData\Local\Programs\Python\Python311\Lib\site-packages\pip (python 3.11) I’ve tried reinstalling, restarting python, checking pip list to see if it’s instal...
🌐
GitHub
bic-berkeley.github.io › psych-214-fall-2016 › sys_path.html
Where does Python look for modules? — Functional MRI methods
When Python hits the line import a_module, it tries to find a package or a module called a_module. A package is a directory containing modules, but we will only consider modules for now. A module is a file with a matching extension, such as .py.
🌐
PyTutorial
pytutorial.com › troubleshooting-python-import-errors
PyTutorial | Troubleshooting Python Import Errors
May 10, 2025 - Use absolute imports when possible. from .submodule import function # Might fail when run as script · For more details, see our Python relative imports guide. Python might not find modules if they're not in your path.
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
🌐
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 - Remember that import errors often have simple solutions once you understand where Python looks for modules. Start by checking if the package is installed, then verify your project structure, and finally ensure you’re using the correct Python environment. When all else fails, the debug function above can help identify the root cause.
🌐
Python
docs.python.org › 3.10 › reference › import.html
5. The import system — Python 3.10.22 documentation
Finders do not actually load modules. If they can find the named module, they return a module spec, an encapsulation of the module’s import-related information, which the import machinery then uses when loading the module.
🌐
Quentin Fortier
fortierq.github.io › python-import
How to import local modules with Python - Quentin Fortier
May 9, 2021 - Module: Python file meant to be imported. Package: directory containing modules/packages. ... ~/Documents/code/project$ python3 src/a/b.py Traceback (most recent call last): File "/home/qfortier/Documents/code/project/src/a/b.py", line 4, in <module> import src.c.d ModuleNotFoundError: No module named 'src'
🌐
Real Python
realpython.com › videos › why-cant-python-find-my-modules
Why Can't Python Find My Modules? (Video) – Real Python
You can learn more about virtual environments in Working With Python Virtual Environments. 00:00 A common error that new Pythonistas will come across is that the packages they think they’ve installed are not actually being recognized by Python. This will present itself as an ImportError, meaning that the module you’ve tried to import cannot be located.
Published: May 26, 2020
🌐
YouTube
youtube.com › derek arends
Python Unable To Find Module - YouTube
This demo is to show a simple way of telling the python interpreter what directories should be searched while running the modules.If I am missing something o...
Published: May 11, 2023
Views: 290
🌐
PythonAnywhere
help.pythonanywhere.com › pages › DebuggingImportError
Debugging with sys.path / ImportError issues | PythonAnywhere Help
May 13, 2015 - Or, if you're using a virtualenv, ... errors), you'll need to fix them. If it loads OK, it will drop you into a Python shell. Try doing the import manually at the command line....
🌐
Quora
quora.com › I-cant-import-a-module-a-py-file-in-my-Python-shell-What-are-some-ideas-Could-it-be-a-wrong-directory
I can't import a module (a .py file) in my Python shell. What are some ideas? Could it be a wrong directory? - Quora
Answer (1 of 3): Let's assume we have two python files. FileA.py & FileB.py You want to import FileA inside FileB. Case 1: Both FileA.py & FileB.py exist in the same directory. [code]# FileB.py # To import FileA in FileB.py use the below statement import FileA [/code]Case 2: FileA.py exist in...