Report this

What is the reason for this report?

No module named error when importing installed libraries

Posted on October 5, 2022

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..."

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.