Reputation: 41
I am deploying a web application onto an AWS EC2 instance, and I'm getting an error. The logs indicate that I do not have cv2 installed.
ModuleNotFoundError: No module named 'cv2'
However, if I ssh into my instance, and run python from the shell I can import no problem.
https://drive.google.com/drive/folders/1-w3BN9pMAhkiDM40fODCPdjvU1Nx71UT?usp=sharing
I have already installed opencv onto the Linux server and checked that it is available for import.
From my application.py file
import cv2
File "/opt/python/current/app/localize.py", line 9, in
but from the command line:
>>> import cv2
>>> cv2.__version__
'4.1.0'
I expected the import to work since it works from the command line.
Upvotes: 2
Views: 2008
Reputation: 41
As @rayryeng suggested, I'm running Python 3.x from Elastic Beanstalk, and Python 2.x from command line. I fixed it by installing the correct version of cv2 for Python 3 and including the following before my import:
import sys
sys.path.append('/usr/local/lib64/python3.6/site-packages')
Upvotes: 1
Reputation: 450
Check if your python package is available for the root/admin user but not accessible for the user trying to run the code? If you can import that module in your EC2, then it is installed, but more importantly for which user it is installed, and for which version. First try : chmod 755 on all directories in python path for your default python and see if it works.(This will provide permissions for all import libraries in Python) If your script is running Python3.7 and Default is Python2.7 then you might have to do -- sudo pip3 install opencv-python
the way to check the version defaults is:
which python ---Will provide default python path and version
which pip ---- Will Provide default PIP details
Upvotes: 1
Reputation: 896
Try this:
cd
wget https://github.com/opencv/opencv/archive/3.2.0.zip
virtualenv project
source project/bin/activate
pip install numpy
mkdir local
unzip opencv-3.2.0.zip
cd opencv-3.2.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_SHARED_LIBS=NO -D WITH_FFMPEG=ON -D BUILD_opencv_python2=ON -D CMAKE_INSTALL_PREFIX=~/local ~/opencv-3.2.0
make
make install
cp ~/local/lib/python2.7/site-packages/cv2.so ~/project/lib64/python2.7/site-packages/
Read more from here: Source
Upvotes: 0