Python – ModuleNotFoundError thrown when calling a function that imports another module in the same directory
Question:
I have the following Python file structure.
-- main.py
-- scripts/
-- __init__.py
-- helper_functions.py
-- mytest.py
mytest.py
has the following function.
def test():
print("test")
helper_functions.py
imports the mytest module and use the test function as
import mytest
def hello():
mytest.test()
if __name__=="__main__":
mytest.test()
__init__.py
imports the hello function from helper_functions.py module so that the main.py outside the scripts folder can directly use the hello function.
from scripts.helper_functions import hello
main.py
tries to call the hello function directly:
from scripts import hello
if __name__=="__main__":
hello()
When I tried to run main.py
, the following error was thrown:
Traceback (most recent call last):
File "main.py", line 1, in <module>
from scripts import hello
File "scripts__init__.py", line 1, in <module>
from scripts.helper_functions import hello
File "scriptshelper_functions.py", line 8, in <module>
import mytest
ModuleNotFoundError: No module named 'mytest'
It seems the mytest module couldn’t be imported into helper_functions.py. How can this be fixed?
Answers:
In helperfunctions.py
replace your code:
import mytest
def hello():
mytest.test()
if __name__=="__main__":
mytest.test()
As following:
from scripts import mytest
def hello():
mytest.test()
if __name__=="__main__":
mytest.test()
Your main.py and mytest.py are not in the same directory.
To solve the error either put main.py in the same directory as mytest.py or replace import mytest
with from scripts import mytest
in helper_functions file. Then main.py will be able to access mytest.py. Modified helper_functions.py:
try:
import mytest
except:
from scripts import mytest
def hello():
mytest.test()
if __name__=="__main__":
mytest.test()
The try
and except
block checks if mytest.py is in the same directory as the file that calls it. If it is in the same directory, then, import mytest
is used. Otherwise from scripts import mytest
is used.
I have the following Python file structure.
-- main.py
-- scripts/
-- __init__.py
-- helper_functions.py
-- mytest.py
mytest.py
has the following function.
def test():
print("test")
helper_functions.py
imports the mytest module and use the test function as
import mytest
def hello():
mytest.test()
if __name__=="__main__":
mytest.test()
__init__.py
imports the hello function from helper_functions.py module so that the main.py outside the scripts folder can directly use the hello function.
from scripts.helper_functions import hello
main.py
tries to call the hello function directly:
from scripts import hello
if __name__=="__main__":
hello()
When I tried to run main.py
, the following error was thrown:
Traceback (most recent call last):
File "main.py", line 1, in <module>
from scripts import hello
File "scripts__init__.py", line 1, in <module>
from scripts.helper_functions import hello
File "scriptshelper_functions.py", line 8, in <module>
import mytest
ModuleNotFoundError: No module named 'mytest'
It seems the mytest module couldn’t be imported into helper_functions.py. How can this be fixed?
In helperfunctions.py
replace your code:
import mytest
def hello():
mytest.test()
if __name__=="__main__":
mytest.test()
As following:
from scripts import mytest
def hello():
mytest.test()
if __name__=="__main__":
mytest.test()
Your main.py and mytest.py are not in the same directory.
To solve the error either put main.py in the same directory as mytest.py or replace import mytest
with from scripts import mytest
in helper_functions file. Then main.py will be able to access mytest.py. Modified helper_functions.py:
try:
import mytest
except:
from scripts import mytest
def hello():
mytest.test()
if __name__=="__main__":
mytest.test()
The try
and except
block checks if mytest.py is in the same directory as the file that calls it. If it is in the same directory, then, import mytest
is used. Otherwise from scripts import mytest
is used.