By Bob Murdoch
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
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!
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.