By Rory McEwan
I’m trying to deploy a Python function to Functions that requires packages to be imported.
The following is the tree of my directory
ingest-attempt2
│ .gitignore
│ project.yml
│
├───.deployed
│ versions.json
│
└───packages
└───sample
└───hello
requirements.txt
__deployer__.zip
__main__.py
This is my requirements.txt
numpy
…and this is my project.yml
parameters: {}
environment: {}
packages:
- name: sample
shared: false
environment: {}
parameters: {}
annotations: {}
functions:
- name: hello
binary: false
main: "main"
runtime: python:default
web: true
webSecure: false
parameters: {}
environment: {}
annotations: {}
limits: {}
When I run the function in DigitalOcean, I get the following error
2025-09-02T20:09:16.864955434Z stderr: Invalid function: No module named 'numpy'
I can see that someone has asked a similar question here but I don’t think I have made a mistake with my folder names as this person has.
Please could someone enlighten me?
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!
Hey!
If I remember this correctly the DigitalOcean Functions platform doesn’t automatically install from requirements.txt. You also need a build.sh script in the same folder as your __main__.py and requirements.txt.
For example, inside packages/sample/hello/ you’d have:
__main__.py
requirements.txt
build.sh
Your requirements.txt:
package-name==1.2.3...
Your build.sh:
#!/bin/bash
set -e
virtualenv --without-pip virtualenv
# For Python 3.11 runtime
pip install -r requirements.txt --target virtualenv/lib/python3.11/site-packages
Then deploy with:
doctl serverless deploy .
This way the build system creates a virtualenv and installs your deps so they’re available to your function.
Heya, @donught
Python functions with external dependencies not provided by the runtime can be deployed with a build script and a requirements.txt file.
First make sure that the file containing the handler function is named __main__.py.
Regards
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.