Skip to content
Back

Python Function: How to import from a companion module?

  • 0
  • Self Hosted
  • Functions
Rando
3 Apr, 2024, 20:56

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.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more