OpenCV installation problems for Python - python

I am trying to install OpenCV on my Ubuntu system. I followed the steps of this page. I am able to run the demos for C/C++ etc. But when I try to run the python demos, I get an ImportError: No module named cv2
Here is more info :
python --version
Python 2.7.8 :: Anaconda 2.1.0 (64-bit)
python -c 'import sys; print sys.path'
['', '/home/radar/anaconda/lib/python2.7/site-packages/pydy-0.2.1-py2.7.egg', '/home/radar', '/home/radar/opencv-2.4.9/modules/python/src2', '/home/radar/anaconda/lib/python27.zip', '/home/radar/anaconda/lib/python2.7', '/home/radar/anaconda/lib/python2.7/plat-linux2', '/home/radar/anaconda/lib/python2.7/lib-tk', '/home/radar/anaconda/lib/python2.7/lib-old', '/home/radar/anaconda/lib/python2.7/lib-dynload', '/home/radar/anaconda/lib/python2.7/site-packages', '/home/radar/anaconda/lib/python2.7/site-packages/PIL', '/home/radar/anaconda/lib/python2.7/site-packages/Sphinx-1.2.3-py2.7.egg', '/home/radar/anaconda/lib/python2.7/site-packages/runipy-0.1.1-py2.7.egg', '/home/radar/anaconda/lib/python2.7/site-packages/setuptools-5.8-py2.7.egg']

You are running Anaconda Python distribution, but you need to use native python to access openCV library. Run Python with specified path and it should work:
/usr/bin/python
Also note, that to use openCV with virtual environmet, you need to manually copy openCV running objects (cv.py, cv2.so) to the environment you are going to use from the dist-packages of your native python, default path is /usr/local/lib/python2.7/dist-packages/.

Related

Getting MATLAB R2019b to use Python3.6

I am trying to use python 3.6 within the MATLAB interpreter. I have both Python 2.7 and 3.6 installed on my machine. When using the default python 2.7 MATLAB work fine:
>> pyenv('Version', '/usr/bin/python')
ans =
PythonEnvironment with properties:
Version: "2.7"
Executable: "/usr/bin/python"
Library: "libpython2.7.so.1.0"
Home: "/usr"
Status: NotLoaded
ExecutionMode: InProcess
>> py.sys.version
ans =
Python str with no properties.
2.7.5 (default, Aug 7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
However when following MATLAB's directions to change versions of python by entering the path of the python 3 executable file, python will not load:
>> pyenv('Version', '/opt/rh/rh-python36/root/usr/bin/python3.6')
ans =
PythonEnvironment with properties:
Version: "3.6"
Executable: "/opt/rh/rh-python36/root/usr/bin/python3.6"
Library: ""
Home: "/opt/rh/rh-python36/root/usr"
Status: NotLoaded
ExecutionMode: InProcess
>> py.sys.version
Unable to resolve the name py.sys.version.
I do notice that the library field in pyenv is not filled in for python 3. So it appears that MATLAB cannot find a python 3 shared object file. Why would MATLAB have problems finding the libpython shared object file?
For me installing libpython3.6 solved this problem, i.e. sudo apt install libpython3.6 on my system. Now the Library field in pyenv is populated and I am able to run python commands.
First of all leave python version installation over to anaconda when it comes to matlab.. and work from within python with your matlab scripts. The next steps to get your python/matlab combo (or visa versa) up and running:
conda install matlab_engine
import matlab.engine
If you hit the bumper:
PackageNotFoundError # (it doesn't install).
Open the Anaconda prompt (be sure you are in desired python version environment (e.g. not base but matpy37):
a) cd matlabroot\extern\engines\python or e.g. cd /Applications/MATLAB_R2019a.app/extern/engines/python on MacOS. (# change to your matlab version if needed).
b.1) and type and run python setup.py install or in some cases bullet b2).
b.2) python setup.py install --prefix "<anaconda installation directory>".
If you don't have for example the proposed conda matpy37 test environment you can create it as followed:
i. conda create -n myenv python=3.6.9 # where e.g. myenv = matpy37. When install done then:
ii. type conda activate matpy37 and run from e.g. Ipython your matlab script by starting it form Jupyter Notebook (Windows: start -> find folder anaconda -> icon Jupyter Notebook (matpy37). Not sure where they hide it in your linux/ubuntu OS.
... in the worse case scenario there are two options (4 and 5):
Don't install the python versions via pip but via anaconda if you want to stay away from breaking package dependencies, fixing callbacks, loggers, diving into subprocess errors and exhausting manual updates and/or down-grades due to package incompatabilities, and more...
Why anaconda:
You can save your specific environment and its package version into a yml-file and recreate it on another PC/Laptop/OS system. The .ylm is in most cases less than 1Mb. So it fits on an usb-stick (some of us call it.. a thumb-drive ;-).
... and possible a last resort (never had to use this myself):
matlab package condalab. Its used for easy switch within matlab.

Issue with two versions of python

Problem having two versions of python on my Mac and them interacting. I have the following python locations
python is /anaconda3/envs/fenicsproject/bin/python
python is /usr/bin/python
and when I try to run a script. I get the following error:
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6
I have googled some solutions and have found some posts saying I should try
env PYTHON_CONFIGURE_OPTS="--enable-framework" #or
env PYTHON_CONFIGURE_OPTS="--enable-shared"
This is they type of code I try to run
#Import packages
import dolfin as dl
I installed the env fenics by following the directions here
The google search possibly found Homebrew + Python on mac os x 10.8: Fatal Python error: PyThreadState_Get: no current thread importing mapnik however I was unable to find the library that links to a wrong version of python using otool.
I also found https://github.com/enthought/mayavi/issues/552 which suggests pinning to a different version of python.
Based on the install notes for hippy, https://hippylib.readthedocs.io/en/latest/installation.html then where they say conda create -n fenicproject ... you need to substitute the following:
conda create -n fenicsproject python==3.5.1
conda install -n fenicsproject -c conda-forge fenics==2017.2.0 \
mpi4py matplotlib scipy sympy==1.1.1 jupyter
After this, python -c 'import dolfin' no longer fails. It might also be possible to use a later version of python (I only tried 3.5.1).
What OS are you using? That will largely determine the specifics of how to go about correcting this issue, but the key here is which Python version the system path points to and what Python version your IDE is pointing to.
What you largely want to avoid is a situation where you are running Python scripts via your native system Python (2, likely).
Check which version of Python your IDE is using (from: How do I check what version of Python is running my script?):
import sys
print(sys.version)
Is the first number a 2? Did you want to use Python 2?
Next, let's check what version your system currently defaults to. If Ubuntu/Linux, use:
python -V
Is this expected? If not, you may need to change your system environmental variables to point to the correct Python version. The solution to this is OS dependent. If Windows, search "Edit Environmental Variables for Your Account" -> "Environmental Variables" -> "Path" , be sure it either points to Anaconda or the correct Python version; if Ubuntu/Linux, check your .bashrc file:
gedit ~/.bashrc
to see if the system points to the correct Python variable. If using a Mac, I formally apologize.

Setting python for RStudio under Anaconda

Can't change python being used when install_keras run from RStudio within Anaconda
I have several versions of Python installed on my Windows 10 box: Python 2.7, Python 3.6, plus the pythons that appear to have been installed with Anaconda. I have RStudio installed via Anaconda.
I am trying to use the keras library in my R code. I run 'install_keras()' and receive the following error:
Error: Error 1 occurred while checking for python architecture
In addition: Warning message:
running command '"C:\Python27\/python.exe" -c "import sys; import platform;
sys.stdout.write(platform.architecture()[0])"' had status 1
I have tried running install_keras using the method and conda settings:
install_keras(method="conda", conda="C:/Development/Anaconda35/Scripts")
But I still get the same error. I have also tried resetting the PATH and RETICULATE_PYTHON environmental variables, using Sys.setenv and use_python() and it doesn't seem to help.
Sys.which('PYTHON') does show the correct executable that I want to use.
Yes, my environment variables did have that Python set first in the path. But changing that didn't seem to help.
I have tried uninstalling and reinstalling keras, both the R library and the 'conda' (pip) python library. That didn't help.
I'm assuming at this point that there's some configuration or setting file with this set, but I can't seem to find it.
Suggestions?

Opencv for python 3.5.2

We seem to be running into a problem downloading opencv which is a dependency for gym-gazebo. Seems opencv works for Python 2.7 but gym-gazebo requires Python 3.5.
We tried fixing it by going into the bash file and tweeking a bit with Python path. We got to a point where opencv can be imported but rospy which is an essential module won't get imported.
We have this error :
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type
Any advice?
Python modules with binary code need to be built for the specific python version.
Install opencv for python 3.5 using whatever python package manager is on your system (conda / pip etc).
If you are on windows you might want to bookmark Unofficial python builds

Error on import caffe

For DeepDream or other deeplearning project, constructing environment of Caffe.
I installed required packages for PyCaffe and made PYTHONPATH to caffe/python.
However when I import caffe on python:
import caffe
Error occurred as below.How to solve this problem?
Segmentation fault: 11
Are you using a mac? I had a very hard time making pycaffe on mac, until I realized that there is a native python installed on all macs and that I was using another version I had installed. While compiling, caffe was using some of the stuff from the native python, and some of the stuff from the other python. I had to make sure to change all the relevant paths in the makefile.config file and to change the python my bash was using. I recommend working in a virtual environment as well. This is a good link to help you out, good luck!
This has been discussed since 2015 in Github issue.
Mostly the reason is the conflict of homebrew python and OS X system python.
Homebrew has provided an awesome solution for OS X:
$ python -V # system Python interpreter
$ python2 -V # Homebrew installed Python 2 interpreter
$ python3 -V # Homebrew installed Python 3 interpreter (if installed)
So the solution is changing all the python paths to python2. Bellowing is my Makefile.config related:
# ...
# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
# PYTHON_INCLUDE := /usr/include/python2.7 \
# /usr/lib/python2.7/dist-packages/numpy/core/include
# ------ For Homebrew installed python. Numpy path is added using python commands.
PYTHON_INCLUDE := /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7
# We need to be able to find libpythonX.X.so or .dylib. ------ (Update Homebrew path)
# PYTHON_LIB := /usr/lib
# PYTHON_LIB := $(ANACONDA_HOME)/lib
PYTHON_LIB := /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib
# Homebrew installs numpy in a non standard path (keg only) ------ (python2 for brew instead of python for system)
PYTHON_INCLUDE += $(dir $(shell python2 -c 'import numpy.core; print(numpy.core.__file__)'))/include
PYTHON_LIB += $(shell brew --prefix numpy)/lib
# ...
Try to manually set python path in your python script if you get no module named caffe error
Ex.
import sys
sys.path.insert(0, "/home/nviso/GitHub/caffe/distribute/python")
import caffe
This generally works for me.
Adding the caffe or python path to .bashrc manually should probably solve the issue as well though not sure, don't have my Office PC now to try :)

Categories