Reputation: 81
I'm trying to upload a function to IBM Cloud Functions with a virtualenv that has opencv installed. However, when I try to run the action in IBM Cloud it says:
{
"error": "Traceback (most recent call last):
File \"/action/1/src/exec__.py\", line 43, in <module>
from main__ import main as main
File \"/action/1/src/main__.py\", line 1, in <module>
import requests, base64, json, cv2\nModuleNotFoundError: No module named 'cv2'"
}
I'm using the python:3.7 runtime for this. I thought this was a library issue since this runtime uses Debian Stretch and I've had problems importing opencv with the python:3-slim-strech docker image before since it didn't have some required libraries like libsm6, libxext6 and libxrender.
However, when I ran apt list
in the docker image that IBM uses for its python:3.7 runtime, it had those libraries included.
I created the virtualenv using the docker method shown here. The exact command I used was the following:
docker run --rm -v "$PWD:/tmp" ibmfunctions/action-python-v3.7 /bin/bash -c
"cd tmp; virtualenv virtualenv; source virtualenv/bin/activate;
pip install --no-deps opencv-python;"
I used --no-deps because the runtime already has numpy installed, which is the only dependency of opencv and because with numpy included the zip file exceeded the 48MB limit to upload it to Cloud Functions.
I should be able to import cv2 with no problems but I still get the previous message. Any help would be great!
Upvotes: 0
Views: 367
Reputation: 4339
Using a virtualenv folder to include local packages does not automatically inherit the global site-packages from the runtime. This can be enabled using the --system-site-packages
flag when using the virtualenv command.
Change the Docker command to the following to make this work:
docker run --rm -v "$PWD:/tmp" ibmfunctions/action-python-v3.7 /bin/bash -c
"cd tmp; virtualenv --system-site-packages virtualenv; source virtualenv/bin/activate;
pip install opencv-python;"
--no-deps
is no longer needed as the numpy dependency is already satisfied by the global site-package.
Following your commands with this updated Docker script now works for me.
Make sure you allocate enough memory to the OpenWhisk action. I had issues running the code with the default 256MB memory limit. Increasing this to 1024MB fixed any issues I encountered.
Upvotes: 3