Report this

What is the reason for this report?

Problems with Python imports in functions

Posted on September 20, 2022

Hi, I’m working with serverless functions and being new to Python having some issues with making my own class imports that work locally also work when deployed to DO.

It could be the way that I am invoking my

packages/joke/joke/___main___.py

file to test it locally.

My file structure matches this example project https://github.com/digitalocean/sample-functions-python-jokes and all my custom classes are in packages/joke/joke alongside the __main__.py file.

I’ve tried both

from packages.joke.joke.persist_email_to_storage import PersistEmailToStorage
from packages.joke.joke.s3_client_exception import S3ClientException

or

from .persist_email_to_storage import PersistEmailToStorage
from .s3_client_exception import S3ClientException

in __main__.py and while either of those works “locally” it does not when deployed.

When I say locally, I’ve created a runlocal.py file in the root directory in attempt to invoke the

__main__.py

file and it does run the code.

import packages.joke.joke.__main__ as fetch_emails

output = fetch_emails.main(args={})
print(output)
print('run local.py complete.')

Is there a better way to emulate how DO would invoke it, where the imports would work in the same way? What would I need to do differently? Thanks

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.