vs code not selecting the right python interpreter - python-3.x

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.

Related

Unable to import Pandas in IPython 6.5.0 console (Spyder)

After installing pandas am able to import in cmd as below :
C:\Users\me\Desktop\Django_Project>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>>
But when am importing pandas in Spyder in IPython 6.5.0 console I get below error:
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 6.5.0 -- An enhanced Interactive Python.
c:\program files (x86)\python37-32\lib\site-packages\ipykernel\parentpoller.py:116: UserWarning: Parent poll failed. If the frontend dies,
the kernel may be left running. Please let us know
about your system (bitness, Python, etc.) at
ipython-dev#scipy.org
ipython-dev#scipy.org""")
In [1] : import pandas
Traceback (most recent call last):
File "<ipython-input-1-38d4b0363d82>", line 1, in <module>
import pandas
ModuleNotFoundError: No module named 'pandas'
In [2] :
Note: I have installed python in "C:\Users\me\AppData\Local\Programs\Python\Python37\" path and environment variables is set as "C:\Users\me\AppData\Local\Programs\Python\Python37;"
And I installed pandas using PIP
Please suggest the solution to resolve this issue.I have tried reinstalling the pandas package almost 6-7 times.
I think you should open anaconda command prompt by searching it in windows search bar . Now write there "conda install -c anaconda pandas" and try again to run the program

import cv2 on python3

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 .

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.

why can't import pandas after installed successfully?

I have installed pandas with command 'pip3.4 install pandas'.
Successfully installed pandas python-dateutil pytz numpy six
Cleaning up...
root#hwy:~# python3.4
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'pandas'
Why can't import pandas in python3.4 after pandas been installed successfully?
root#hwy:/home/debian8# pip3.4 show pandas
---
Name: pandas
Version: 0.17.1
Location: /usr/local/python3.4/lib/python3.4/site-packages
Requires: python-dateutil, pytz, numpy
root#hwy:/home/debian8# echo "import sys; print sys.path"
import sys; print sys.path
root#hwy:/home/debian8# python3.4
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu',
'/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages',
'/usr/lib/python3/dist-packages']
Your pandas is installed here:
/usr/local/python3.4/lib/python3.4/site-packages
But this path is not in sys.path.
As a workaround do:
export PYTHONPATH=$PYTHONPATH:/usr/local/python3.4/lib/python3.4/site-packages
and inside this terminal start Python again and do your import of pandas.
If this works, add this line above (export PYTHONPATH...) to your ~/.bashrc or equivalent if you use a different shell for a more permanent solution.

Resources