Question

No module named error when importing installed libraries

Trying to deploy a python script as Function, this script has dependencies. Using the sample code (https://github.com/digitalocean/sample-functions-python-sendgrid-email), I can deploy my script as long as I don’t import any of the dependencies.

If I try to import library that was successfully installed I get this error 2022-10-05T19:04:10.385463002Z stderr: Invalid action: No module named 'pdfrw'

__main__.py

import os, json, sys
import pdfrw
from datetime import date

def main(args):
      name = args.get("name", "stranger")
      greeting = "Hello  " + name + "!"
      return {"body": os.getcwd() + ':' + os.environ['PATH']}

build.sh

#!/bin/bash
set -e
virtualenv venv
source venv/bin/activate
python3 -m pip install -r requirements.txt
deactivate

requirements.txt

pdfrw

Result from build.sh

created virtual environment CPython3.9.14.final.0-64 in 3727ms"
 creator CPython3Posix(dest=/tmp/slices/builds/fn-0c691d34-dc60-4292-a56b-be4aa0adddbe/sample_hello..."
 seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data..."
 added seed packages: pip==21.1.2, setuptools==57.0.0, wheel==0.36.2"
 activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshAc..."
Collecting pdfrw"
 Downloading pdfrw-0.4-py2.py3-none-any.whl (69 kB)"
Installing collected packages: pdfrw"
Successfully installed pdfrw-0.4"
WARNING: You are using pip version 21.1.2; however, version 22.2.2 is available."
"You should consider upgrading via the '/tmp/slices/builds/fn-0c691d34-dc60-4292-a56b-be4aa0adddbe/sa..."


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.

Pinned Answer

I found the problem. I had to use virtualenv as the name instead of venv

This is not working for me. I have deployed many functions with libraries like pymongo and everything worked perfectly fine.

now I working with SQLAlchemy and I am alway reciving the error that No Module Found.

requirements.txt SQLAlchemy==2.0.20

Build.sh #!/bin/bash

set -e

virtualenv pnpai source pnpai/bin/activate

pip install -r requirements.txt deactivate

The same structure with different library seemed to work, but no idea why it is not working with SQLAlschemy.

Any help?

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.