Question

Problems with Python imports in functions

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


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Rodric Rabbah
DigitalOcean Employee
DigitalOcean Employee badge
September 26, 2022
Accepted Answer

I created this sample repo https://github.com/rabbah/functions/tree/master/pymod if it’s helpful. Are you relying on any pip installed packages that may be globally installed on your dev environment? Can you share the error you’re encountering?

If there is a third party library (S3 related perhaps?) then you will need a build file to create a Python virtual environment that captures the dependencies similar to what’s done for the jokes sample project.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

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

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.