My main function is named main.py but I also have a utility.py (in the same directory) for my function.
For testing, my utility.py only contains:
def simple_test():
print("Simple test function in utility.py")
In my main.py, I am trying to import with:
from utility import simple_test
But it fails during exectution with:
File "/usr/local/server/src/function/main.py", line 18, in <module>
from utility import simple_test
ModuleNotFoundError: No module named 'utility'
What is the correct syntax for importing from a separate file?
TL;DR
To import a function from a companion module in Python correctly, ensure both files are in the same directory. In your main.py, use the syntax:
from utility import simple_test
This error can occur if the Python interpreter can't locate the utility module. Double-check the file paths and make sure there is an __init__.py file present in the directory.