Opencv will not import - python

I ran pip3 install opencv-python==3.3.0.10
but I am getting
$ python3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
I know there are many ways to install opencv but this one seemed the most straightforward.

1 - Open a terminal and pip install cmake:
pip install cmake
2 - Download opencv repository from https://github.com/opencv/opencv
3 - On your terminal cd to the folder where you downloaded the opencv repository and compile it with cmake typing the following commands:
mkdir release
cd release
cmake -D CMAKE_BULD_TYPE=RELEASE -D BUILD_PYTHON_SUPPORT=ON USE_V4L=ON WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON USE_GStreamer=ON ..
make
make install
Opencv is now correctly installed on your machine and you should be able to successfully import it in Python with no error.
Let me know if this solves the problem

Related

PyTorch and OpenCV not working in same python

OpenCV and PyTorch can not be imported with the same python version.
I have installed Pytorch through anaconda and OpenCV from source build .so both can not be imported at the same time:
somnath#somnath-Inspiron-5558:~/LaneDetection/lanedet$ python3
Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import torch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
>>> exit()
somnath#somnath-Inspiron-5558:~/LaneDetection/lanedet$ conda activate
(base) somnath#somnath-Inspiron-5558:~/LaneDetection/lanedet$ python3
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
You need to make sure that you specify the correct options to cmake when building for your specific conda env.
Activate your conda env conda activate <name>
Install numpy with conda install numpy
Run cmake from the build directory using these options:
cmake \
-D CMAKE_INSTALL_PREFIX=<your choice> \
-D BUILD_opencv_python2=0 \
-D PYTHON3_LIBRARY=${CONDA_PREFIX}/lib/libpython3.9.so \
-D PYTHON_INCLUDE_DIR=${CONDA_PREFIX}/include/python3.9/ \
-D PYTHON3_EXECUTABLE=${CONDA_PREFIX}/bin/python \
-D PYTHON3_PACKAGES_PATH=${CONDA_PREFIX}/lib/python3.9/site-packages \
<path to opencv source>
CONDA_PREFIX contains the directory of the currently active environment., change python3.9 in case you have another python version.

Can't Import Django using Python3 on macOS Sierra

Have both versions of Python (Legacy 2.7.10 and 3.6.2) installed on macOS Sierra.
Installed pip using the following steps.
Downloaded it using curl:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Installed pip it by running the installer script:
sudo python get-pip.py
Checked for any upgrades / updates for pip:
sudo pip install -U pip
Installed django 1.11:
sudo pip install django==1.11
When I run python (legacy):
python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
1.11
However, when trying it using python3:
python3
Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'django'
Note: I followed a different tutorial and got python3 working with django 1.11 using virtualenv - please don't suggest this as I am new to the python world and just want to use python3 / django 1.11 in a non-virtual environment - I just want have it working like the legacy python interpreter is behaving.
pip installs libraries differently for python2 and python3, so you effectively have different environments for each. If you want to install Django for python3, you'll want to install it like this:
pip3 install django==1.11

python package installed but could not import

I have tried to re-install and got the following message:
Requirement already satisfied: troposphere==1.8.2 in /usr/local/lib/python2.7/site-packages (from -r requirements.txt (line 13)
I checked my python version and I could see this as same:
animjain$ python -V
Python 2.7.10
When I try to import a module, get the following error:
animjain$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from troposphere import Join, Ref, FindInMap
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named troposphere
You should use virtualenv to install dependencies for your project. You don't need to clog global site-packages.
For example:
sudo apt-get install virtualenv
cd ~
virtualenv your_test_venv
source your_test_venv/bin/activate
pip install troposphere
After that you can use your new package.
When you want to deactivate your virtualenv you can run command deactivate
In case you have multiple Python versions on your machine, install with the same Python command, so instead of:
animjain$ pip install troposphere==1.8.2
run:
animjain$ python -mpip install troposphere==1.8.2

Segmentation fault and crashing when trying to import opencv

I have mac os x 10.9. I downloaded opencv-python using homebrew and I have both the python 2.7 and python 3.4 versions off of the main python site, downloaded the usual way with macs. I need to use opencv, but they do not have a download package for macs so I used homebrew:
brew tap homebrew/science
brew install opencv
When I type python into the terminal, and then type import cv, I get a segmentation error and python quits unexpectedly. The same thing happens in IDLE. For python 3.4, I get a no module error. Here it is:
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/aurora_alvarez-buylla/cv.py", line 1, in <module>
from cv2.cv import *
ImportError: No module named 'cv2'
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
Segmentation fault: 11
and then it quits unexpectedly. Does anyone know what is going on? Frankly I have spent way too much time on this opencv issue and am finding it very frustrating. Thank you!
Chris Muktar pointed in this thread that the problem is caused by the conflict between the system Python and brewed Python. Following that idea, I resolved this issue by removing the system python and re-installing opencv:
$ cd /usr/bin
$ sudo mv python python.bak
$ brew uninstall opencv
$ brew install opencv

installing cx_Oracle on Centos 6

I'm having a heck of a time trying to get cx_Oracle installed.
I've tried various versions of the Oracle libraries. The ones I get closest with the 32-bit 11.1 libraries
oracle-instantclient11.1-basic-11.1.0.7.0-1.i386.rpm
oracle-instantclient11.1-devel-11.1.0.7.0-1.i386.rpm
oracle-instantclient11.1-sqlplus-11.1.0.7.0-1.i386.rpm
I have the environment variables set to:
[~]# set | grep -i oracle
LD_LIBRARY_PATH=/usr/lib/oracle/11.1/client/lib:
ORACLE_HOME=/usr/lib/oracle/11.1/client/lib
PATH=/usr/lib/oracle/11.1/client/lib:/home/sekingerg/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/.rvm/bin
SQLPATH=/usr/lib/oracle/11.1/client/lib:
and when I run pipinstall, it fails to find the include files:
[~]# pip install cx_Oracle
Downloading/unpacking cx-Oracle
Running setup.py egg_info for package cx-Oracle
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/tmp/pip-build-root/cx-Oracle/setup.py", line 180, in <module>
raise DistutilsSetupError("cannot locate Oracle include files")
distutils.errors.DistutilsSetupError: cannot locate Oracle include files
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/tmp/pip-build-root/cx-Oracle/setup.py", line 180, in <module>
raise DistutilsSetupError("cannot locate Oracle include files")
distutils.errors.DistutilsSetupError: cannot locate Oracle include files
----------------------------------------
Command python setup.py egg_info failed with error code 1 in /tmp/pip-build-root/cx-Oracle
Storing complete log in /root/.pip/pip.log
Here's the rpms that have been installed:
[~]# rpm -qa | grep -i oracle
oracle-instantclient11.1-devel-11.1.0.7.0-1.i386
oracle-instantclient11.1-sqlplus-11.1.0.7.0-1.i386
oracle-instantclient11.1-basic-11.1.0.7.0-1.i386
[~]#
I also tried to install the cx_Oracle rpm: cx_Oracle-5.1.2-11g-py33-1.x86_64.rpm and that didn't work either.
[~]# rpm -ipv cx_Oracle-5.1.2-11g-py33-1.x86_64.rpm
Preparing packages for installation...
cx_Oracle-5.1.2-1
But after that install, it's still not recognized.
[~]# python3
Python 3.3.2 (default, Nov 18 2013, 12:27:03)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'cx_Oracle'
>>>
I wrote this script about one years ago for install cx_Oracle in debian:
TMP_ORACLE_HOME="/usr/lib/oracle/11.2/client64"
dpkg -i $PWD/libpython3.2_3.2.3-2_amd64.deb
dpkg -i $PWD/libaio1_0.3.107-7_amd64.deb
dpkg -i $PWD/oracle-instantclient11.2-basic_11.2.0.3.0-2_amd64.deb
echo "$TMP_ORACLE_HOME/lib" > /etc/ld.so.conf.d/oracle.conf
echo "export ORACLE_HOME=$TMP_ORACLE_HOME" > /etc/profile.d/oracle.sh
dpkg -i $PWD/cx-oracle_5.1.2-2-py32_amd64.deb
cd /usr/lib/python3.2
mv site-packages dist-packages
ln -s dist-packages site-packages
ldconfig
I also encountered this situation. The issue was a permissions issue due to installations requiring "sudo" permissions for "pip install".
NOTE: I'm using oracle linux 6, so mileage may differ
Here is the directory listing where cx_Oracle.so was sent following build so you can see the default permissions (compare to cx_Freeze, for example, which has world access):
[vagrant#oracle /]$ ll /usr/lib64/python2.6/site-packages
total 4308
drwxr-xr-x 5 root root 4096 Jan 17 09:48 cx_Freeze
drwxr-x--- 2 root root 4096 Jan 17 11:30 cx_Oracle-5.1.2-py2.6.egg-info
-rwxr-x--- 1 root root 351475 Jan 17 11:30 cx_Oracle.so
I could identify this issue with the following, which executes python as root, and then "import cx_Oracle" works:
[vagrant#oracle cx_OracleTools-8.0]$ sudo ORACLE_HOME=/usr/lib/oracle/11.2/client64 LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/lib/oracle/11.2/client64/lib python
Python 2.6.6 (r266:84292, Oct 15 2013, 07:32:41)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>
So... I hit it over the head with a hammer... and let my user own it all (which is the wrong solution, admittedly, and may have later fallout.
- I would love to hear the right way to let my vagrant user "pip install"
Hope this help,
J
Nothing I did could resolve the library issue. While Farhadix's suggestion was helpful, I could not get past this error when doing an "import cx_Oracle":
ImportError: libpython3.3m.so.1.0: cannot open shared object file: No such file or directory
I ended up removing the cx_Oracle package (rpm -e cx_Oracle-5.1.2-1.x86_64) and building the module from source following instructions from here. That was what I needed to do to get the extension to work on CentOS 6.4 with python 3.3.

Categories