import cv2 on python3 - python-3.x

I have a problem trying to import cv2 in python3 when I run it from my home folder...
luis#luis-Ubuntu:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type
>>>
but when I run python3 inside /usr/lib/python3/dist-packages or /usr/local/lib/python3.5/dist-packages path it works fine...
luis#luis-Ubuntu:~$ cd /usr/lib/python3/dist-packages
luis#luis-Ubuntu:/usr/lib/python3/dist-packages$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>
I know this is related to include a path on the sys library,
I added the export PYTHONPATH on my ~/.bashrc but didn't solve the problem...
export PYTHONPATH="/usr/local/lib/python3.5/dist-package‌​s:$PYTHONPATH"
I also found that if I insert the path before importing cv2 it works but I need to do this on all scripts or every time I run python3 from a terminal...
luis#luis-Ubuntu:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type
>>> import sys
>>> sys.path.insert(0,'/usr/lib/python3/dist-packages')
>>> import cv2
>>>
but I want to fix this permanently, does anyone know how to solve this...
btw... runs fine on python2...

From the print(sys.path) that you have provided,
...'/opt/ros/kinetic/lib/python2.7/dist-packages'...
I think the problem lies in this item. Although the python3 path also resides in sys.path, the python2.7 path precedes it. Python will catch the one in python2.7 first. When you are running in directly inside /python3/dist-packages, the current directory is placed first, and that precedes python2.7.
I think there are two ways:
Remove python2.7/dist-packages from your PYTHONPATH
Call sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages') before you import cv2. See this question.
The first approach should be the "proper" way but I am not sure how it is deal with if you want to use both python2/3 at the same time. You might be interested in this post.

You can add the following two lines before you import cv2 in your python code. It works for me without changing any source file:
import sys
sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
Then import cv2:
import cv2

The root of this problem are the ROS commands like source/opt/ros/kinetic/setup.bash in the bashrc file /home/username/.bashrc , which force changes in the Python path. Even if you are not using ROS, the commands are still executed and thus you are directed to find cv2 in /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so in the ROS folder, which is not compatible with Python 3. More discussion on the same issue can be seen at:
After install ROS Kinetic, cannot import OpenCV
. Here I propose another solution which is not mentioned by any answer at that post.
The idea is to run source/opt/ros/kinetic/setup.bash(and any other bash related to ROS) only when using ROS. This way you do not have to modify .bashrc file (or editing the PYTHONPATH like to what you do) every time.
First, remove ROS-related commands like source/opt/ros/kinetic/setup.bash from the basrch file and make sure that you can import cv2 in Python 3.x with no error now.
Then, create a environment and install all the ROS related packages here. By doing so, we will have to activate this ros_environment when running ROS. For creating environment, see
https://conda.io/docs/user-guide/tasks/manage-environments.html#creating-an-environment-with-commands
Next, activate your newly-created environment and follow
https://conda.io/docs/user-guide/tasks/manage-environments.html#macos-and-linux
to create files in the suggested path. Namely,
cd $CONDA_PREFIX
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh
Edit the ./etc/conda/activate.d/env_vars.sh as follows
source/opt/ros/kinetic/setup.bash
Also add in any other ROS-related bash in the file.
As for ./etc/conda/deactivate.d/env_vars.sh, what I do is exporting the PYTHONPATH back to Python 3. For example, it could be
export PYTHONPATH="/usr/lib/python3/dist-packages"
Note that this is not really deactivating the source/opt/ros/kinetic/setup.bash command. I just found doing this way would direct my PYTHONPATH back to the default python 3, and the correct cv path can be found even after activation and deactivation of the ROS environment .

Related

ImportError: No module named numpy in Debian OS

In my virtual environment on my debian machine, I am able to import numpy at the prompt But when I call it in a program it errors out.
I am looking over the net and they say to uninstall and re-install. I tried but novail.
Also, I am able to import it in python prompt but not when I call the script? Please help me out of this.
I am able to call successfully at the prompt:-
(venv) root#c3-redsuren-vm01:~/my-project# python
Python 3.8.2 (default, Apr 13 2020, 08:44:45)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
>>> import numpy
>>> exit()
Here when I try to execute my program it errors out:-
(venv) root#c3-redsuren-vm01:~/my-project# ./test_nump.py
Traceback (most recent call last):
File "./test_nump.py", line 2, in <module>
import numpy
ImportError: No module named numpy
(venv) root#c3-redsuren-vm01:~/my-project# cat test_nump.py
#!/usr/bin/python
import numpy
print("test")
(venv) root#c3-redsuren-vm01:~/my-project#
As per request from the OP.
For any executable file, bash reads the first line with the shebang or #!... to extract the interpreter for the script. If it does not find the shebang, it will try to service the file type using the magic numbers and then use the relevant loading method. That's why running an ELF binary does not require you to specify the executor.
Now, virtual environments works by overriding your bash search path, i.e. when you type a command, where should it look. For example, if you have a binary name ls where should it find it to load from. It refers to your search path or the $PATH variable and scans the directories sequentially and stops at the first match. Virtual environment will prepend it's path to the $PATH when you do a source .venv. The source command simply tells your bash session to apply the bash directives from the .venv file.
In your source file, you have supplied the interpreter using the shebang directive to the global installation of python which does not have numpy in it's module list so it fails.
If you run it using python script.py, because the $PATH is overridden by virtual env, the shell will find the python version of virtual env first than the global one which has your numpy module.
Why does /usr/bin/env python work ? Because it's going to refer to your $PATH in your environment variable (which is modified by virtual env) and will find the correct python installation.

vs code not selecting the right python interpreter

I have been using vs code and its data science module for a long time to create and edit python scripts via SSH. Recently I tried to open a jupyter notebook with it but I am having issues with the python interpreter
The python interpreter selected is the one from the base environment from miniconda
However, when I try to import a module that is installed on conda base I am getting
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-094d2d97d2ef> in <module>
1 # imports
2 import pandas as pd
----> 3 import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
I am sure that matplotlib is installed on the base environment. If, on a terminal window, on VS code itself, I
open the same python interpreter (/home/user/miniconda3/bin/python)
call the same import (import matplotlib.pyplot as plt)
I get no error:
(base) user#Brightcore-testsrv:~$ which python
/home/user/miniconda3/bin/python
(base) user#Brightcore-testsrv:~$ /home/user/miniconda3/bin/python
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>>
Your VS Code Jupyter notebook may be running with a different interpreter as its kernel. You can execute this code to check:
import sys
print(sys.executable)
If the result of running the code above is not /home/user/miniconda3/bin/python, you can use the kernel selector to switch kernels to your desired environment.

python3 cannot find a module that I could import with python2

I'm trying to switch from python2 to python3. In the process I'm also switching from anaconda to miniconda3 as my primary package management tool. There are some other packages that I clone from github. I found that I can no longer import any modules from packages downloaded from github rather than conda. For example, import linetools.utils used to work and no longer works. import linetools.linetools.utils works but this is not enough, since various modules inside the package reference each other. This package is not written by me so changing all occurrences of linetools.utils to linetools.linetools.utils is not desirable.
Here are additional information for you to help solve my problem.
My $PYTHONPATH environment variable is set to /Users/lwymarie/python/. I also tried putting this same path to my $path variable as well. Didn't solve my problem.
Here are the packages I have under PYTHONPATH. Just showing a few.
flemish.local> ls $PYTHONPATH
sdsspy Barak desisim linetools desiutil pymc3 specutils PypeIt pyqtgraph statsmodels
RemoteObserving fitsio Ska.Numpy ginga redrock
Here is the expected behavior, when I used python2 and anaconda2. I'm using the package linetools as an example.
flemish.local> python
Python 2.7.12 |Anaconda custom (x86_64)| (default, Jul 2 2016, 17:43:17)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import linetools.utils
Here's the sys.path when the above worked. Just showing a few of the elements.
>>> import sys
>>> sys.path
['', '/Users/lwymarie/python', '/Users/lwymarie/anaconda/lib/python27.zip', '/Users/lwymarie/anaconda/lib/python2.7', '/Users/lwymarie/anaconda/lib/python2.7/plat-darwin', '/Users/lwymarie/anaconda/lib/python2.7/plat-mac', '/Users/lwymarie/anaconda/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/lwymarie/anaconda/lib/python2.7/lib-tk', '/Users/lwymarie/anaconda/lib/python2.7/lib-old', '/Users/lwymarie/anaconda/lib/python2.7/lib-dynload', '/Users/lwymarie/anaconda/lib/python2.7/site-packages', '/Users/lwymarie/anaconda/lib/python2.7/site-packages/PIL', '/Users/lwymarie/anaconda/lib/python2.7/site-packages/Sphinx-1.3.1-py2.7.egg', '/Users/lwymarie/anaconda/lib/python2.7/site-packages/aeosa', '/Users/lwymarie/python/specutils', '/Users/lwymarie/python/ginga', '/Users/lwymarie/python/statsmodels', '/Users/lwymarie/python/Ska.Numpy', '/Users/lwymarie/python/linetools', '/Users/lwymarie/python/pymc3', '/Users/lwymarie/anaconda/lib/python2.7/site-packages/numpydoc-0.6.0-py2.7.egg', '/Users/lwymarie/anaconda/lib/python2.7/site-packages/nbsphinx-0.2.9-py2.7.egg', '/Users/lwymarie/anaconda/lib/python2.7/site-packages/recommonmark-0.4.0-py2.7.egg', '/Users/lwymarie/anaconda/lib/python2.7/site-packages/CommonMark-0.5.4-py2.7.egg', '/Users/lwymarie/anaconda/lib/python2.7/site-packages/joblib-0.10.2-py2.7.egg', '/Users/lwymarie/anaconda/lib/python2.7/site-packages/Theano-0.8.2-py2.7.egg', '/Users/lwymarie/anaconda/lib/python2.7/site-packages/ginga-2.7.0-py2.7.egg', '/Users/lwymarie/anaconda/lib/python2.7/site-packages/QtPy-1.3.1-py2.7.egg', '/Users/lwymarie/anaconda/lib/python2.7/site-packages/desiutil-1.9.9.dev596-py2.7.egg', '/Users/lwymarie/anaconda/lib/python2.7/site-packages/desisim-0.24.0.dev1308-py2.7.egg', '/Users/lwymarie/python/redrock/py', '/Users/lwymarie/python/PypeIt', '/Users/lwymarie/anaconda/lib/python2.7/site-packages']
Exiting Python, here's the $path variable when the above worked.
flemish.local> echo $path
/Users/lwymarie/anaconda/bin . /usr/local/bin /usr/local/etc /opt/local/bin /opt/local/sbin /usr/sbin /sbin /usr/bin /bin /usr/lang /etc /usr/etc /usr/X11/bin /usr/local/scisoft/bin /usr/local/texlive/2020/bin/x86_64-darwin /Library/Ruby/Gems/2.0.0 ./py/ ./
Here is the unwanted behavior, when I used python3 and miniconda3.
flemish.local> python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import linetools.utils
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'linetools.utils'
Here's the sys.path when the above error happened. I also tried the sys.path.append("/Users/lwymarie/python/linetools/") trick and it still couldn't find the module.
>>> import sys
>>> sys.path
['', '/Users/lwymarie/python', '/Users/lwymarie/miniconda3/lib/python37.zip', '/Users/lwymarie/miniconda3/lib/python3.7', '/Users/lwymarie/miniconda3/lib/python3.7/lib-dynload', '/Users/lwymarie/miniconda3/lib/python3.7/site-packages']
Exiting Python, here's the $path variable when the above error happened.
flemish.local> echo $path
/Users/lwymarie/miniconda3/bin /Users/lwymarie/miniconda3/bin . /usr/local/bin /usr/local/etc /opt/local/bin /opt/local/sbin /usr/sbin /sbin /usr/bin /bin /usr/lang /etc /usr/etc /usr/X11/bin /usr/local/scisoft/bin /usr/local/texlive/2020/bin/x86_64-darwin /Library/Ruby/Gems/2.0.0 ./py/ ./
Here's an image of the directory tree of the linetools package.
If you successfully help me get python3 and linetools working, your good deed will be rewarded with eternal life and happiness.
There is '/Users/lwymarie/python/linetools' in your python2 sys.path but not in your python3 sys.path. Did you re-setup linetools using python3?
cd ~/python/linetools
python setup.py develop
I think what Steven Lau suggested should work.
But keep in mind that you have multiple index paths. You have /Users/lwymarie/python and /Users/lwymarie/python/linetools (I see this for your py2 setup).

Tkinter found on Python 3 but not on Python2.7

I am on CentOs7. I installed tk, tk-devel, tkinter through yum. I can import tkinter in Python 3, but not in Python 2.7. Any ideas?
Success in Python 3 (Anaconda):
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>>
But fail on Python 2.7 (CentOS default):
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: libTix.so: cannot open shared object file: No such file or directory
I read some answers said
If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do not edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.
I have reinstalled tk, tk-devel and tkinter through yum, but the problem is same.
How can I configure it to work on Python 2.7?
For python 3 use:
import tkinter
For python 2 use:
import Tkinter
If these do not work install with, for python 3:
sudo apt-get install python3-tk
or, for python 2:
sudo apt-get install python-tk
you can find more details here
For python2.7 try
import Tkinter
With a capital T. It should already be pre-installed in default centos 7 python setup, if not do yum install tkinter

How to give python access to system wide modules in ubuntu?

I have python 2.7.12 installed in Ubuntu 16.04 (64-bit version). I have modules such as numpy, scipy, sympy etc. installed via pip as well. My problem is, when I open python command line via Terminal and try to import these modules, I get the following error:
$ python
Python 2.7.12 (default, Jul 10 2016, 20:42:07)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>>
Upon doing some research, I found from this thread that if I open python command line using /usr/bin/python and try importing these modules, I don't get any errors.
$ /usr/bin/python
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import scipy
>>> import sympy
>>> import matplotlib
>>> import pandas
>>>
But I would like to know if there is any way I can just type in python from Terminal and import these modules in the python command line? For example, if I write a program like this,
x = 2
print x
y = 5
print y
print x+y
import numpy
import scipy
import sympy
save it in a file named test.py in my desktop and open it using the command /usr/bin/python test.py, I am getting the desired output.
$ /usr/bin/python test.py
2
5
7
But if I try the same with the command python test.py, I get the error again
$ python test.py
2
5
7
Traceback (most recent call last):
File "test.py", line 8, in <module>
import numpy
ImportError: No module named numpy
From what I understand, python doesn't have access to system wide modules since it is installed locally. If so, is there a way to make python global or the modules local to python? I have been trying for the past couple of hours to find a solution but I haven't found anything yet and I am new to Linux. Thanks for your help.
I think the root cause is you have several python binary under $PATH, and your system doesn't use /usr/bin/python by default.
run command which python to see which python is used by default
rename the default python file to something like 'python-2-7-12'
then try to run python test.py again to see if it is resolved.

Resources