0

I'm relatively new to Jupyter Notebook and have been struggling with python versions with Jupyter Notebook.

I installed seaborn but import error occurred saying no seaborn package found. It shows on upper right corner of Jupyter "Python 3" but it returned Python 2.7 when I run !python --version. Also when I run print(sys.path), the result is below.

['', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mysql-0.0.1-py3.5.egg', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/IPython/extensions', '/Users/Cynthia/.ipython']

My guess is that my python kernel isn't pointing correctly to python3 although notebook shows it's python3. Could someone pls help me solve this? It would be helpful if there could be code to run in Jupyter cell. Thank you!

Stefan
  • 1,697
  • 15
  • 31
flyunicorn
  • 31
  • 3
  • Did you added python 3 to the path?Check that out. – Irfan wani Aug 23 '20 at 06:58
  • 1
    You should probably look at `sys.version` - `!python --version` could be starting a different python. – alani Aug 23 '20 at 06:59
  • 1
    ``python`` is usually the name of the Python2 executable. Just because you are running this command from inside Python3 does not change that. Check ``sys.executable`` to see what is the executable of the current program. You likely installed seaborn for the wrong Python executable as well. – MisterMiyagi Aug 23 '20 at 07:00
  • Loosely related, when you start messing with different python versions and package versions I would recommend to a package manager like `conda` ([install conda](https://docs.conda.io/projects/conda/en/latest/user-guide/install/)). There is also Anaconda, the GUI based approach ([install anaconda](https://docs.anaconda.com/anaconda/install/)) – Stefan Aug 23 '20 at 07:08
  • @alaniwi when I run sys.version, it says '3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]' – flyunicorn Aug 23 '20 at 08:14
  • @MisterMiyagi when I run sys.executable, it returns '/usr/local/bin/python3' – flyunicorn Aug 23 '20 at 08:15
  • @Irfanwani could you pls show the code how to run in Jupyter cell? – flyunicorn Aug 23 '20 at 08:16
  • @flyunicorn Right, so Jupyter is running the correct python. Why you don't have seaborn installed is some separate problem, but it is not that Jupyter is running python 2. – alani Aug 23 '20 at 08:16
  • @Stefan I did install conda and used conda install to install seaborn, but when I import, it says no such package... – flyunicorn Aug 23 '20 at 08:18
  • When you run `sys.path` in python interpreter after activating your environment, you should see your environment path at the end of `sys.path` list. Looks like your environment weren't activated. Did you activate your environment? (conda, virtualvenv etc.) – harryghgim Aug 23 '20 at 08:22
  • @harryghgim How to activate it? – flyunicorn Aug 23 '20 at 08:23
  • run `conda env list`, where you will see a list of conda environments if you created any. run `conda activate your_env_name_to_activate` to activate. – harryghgim Aug 23 '20 at 08:34
  • @harryghgim it returns # conda environments: # base * /Users/Cynthia/anaconda3 py2 /Users/Cynthia/anaconda3/envs/py2 py27 /Users/Cynthia/anaconda3/envs/py27 – flyunicorn Aug 23 '20 at 08:59
  • @harryghgim I want to use python 3, so why the upper right corner on Jupyter Notebook shows Python 3, whereas python version says in cell Python 2.7, and conda env list shows python 2. I'm so confused... – flyunicorn Aug 23 '20 at 09:05

2 Answers2

1

When you run python --version, It won't spit python 3.5.x, because python refers to python2 unless you aliased python as python3. So it makes sense that you see python 2.7.x when you run python --version.

As for py2 when you run conda env list, they are env names you set. They are just names, not python versions.

What needs to be done I think is to find out where your jupyterlab is installed, which I think in (base) environment. In your base environment, run conda list, where you will see a list like this:

(base) ➜  test conda list
# packages in environment at /Users/gwanghyeongim/.pyenv/versions/miniconda3-latest:
#
# Name                    Version                   Build  Channel
brotlipy                  0.7.0           py38haf1e3a3_1000  
ca-certificates           2020.6.24                     0  
certifi                   2020.6.20                py38_0  
cffi                      1.14.1           py38hed5b41f_0  
chardet                   3.0.4                 py38_1003  
conda                     4.8.4                    py38_0  
conda-package-handling    1.6.1            py38h1de35cc_0  
cryptography              2.9.2            py38ha12b0ac_0  
idna                      2.10                       py_0  
libcxx                    10.0.0                        1  
libedit                   3.1.20191231         h1de35cc_1  
libffi                    3.3                  hb1e8313_2  
ncurses                   6.2                  h0a44026_1  
openssl                   1.1.1g               h1de35cc_0  
pip                       20.2.2                   py38_0  
pycosat                   0.6.3            py38h1de35cc_1  
pycparser                 2.20                       py_2  
pyopenssl                 19.1.0                     py_1  
pysocks                   1.7.1                    py38_1  
python                    3.8.3                h26836e1_1  
python.app                2                       py38_10  
readline                  8.0                  h1de35cc_0  
requests                  2.24.0                     py_0  
#and so on...

See if you see jupyterlab in the list. If so, your jupyter notebook is in (base) environment.

Now the most likely scenario is you installed seaborn in py2 environment. That means you dind't install seaborn in your base environment. Install it by running conda install seaborn or pip install seaborn.

If something didn't work so far, try runnning conda upgrade --all -y to upgrade packages. It might be from collision between deprecated packages.

P.S My suggestion is you create a separate environment and run packages on it.

  1. Run conda create -n your_env_name to do so(replace your_env_name to the name you want set)

  2. Activate by running conda activate the_env_you_just_created

  3. If 2 doesn't work somehow, make sure you run conda init your_shell, where your_shell can be found by running echo $SHELL, where the last word after / is your shell.

  4. Make sure you see (your_env_name) at the first part of command prompt. If so, your env is activated. Now install packages on here and do your project, rather than on base environment.

harryghgim
  • 1,340
  • 4
  • 12
  • 33
  • Thank you so much! I run conda list, and yes jupyterlab is in my base list. But when I run conda install seaborn or pip install seaborn, it shows "Requirement already satisfied: seaborn in /Users/Cynthia/anaconda3/lib/python3.6/site-packages (0.9.0)" So now I'm running conda upgrade --all -y. Hopefully I can import seaborn after this update... – flyunicorn Aug 23 '20 at 12:06
  • It finished running with an error saying "RemoveError: 'setuptools' is a dependency of conda and cannot be removed from conda's operating environment." – flyunicorn Aug 23 '20 at 12:32
  • @flyunicorn [This](https://stackoverflow.com/questions/54392995/removeerror-requests-is-a-dependency-of-conda-and-cannot-be-removed-from-cond) and [this](https://stackoverflow.com/questions/57549872/removeerror-setuptools-is-a-dependency-of-conda-and-cannot-be-removed-from-co) suggest that you run `conda update conda`. Check them out and consider trying it out. – harryghgim Aug 23 '20 at 12:59
  • I run echo $SHELL, it returns /bin/bash. Next I run !conda init bash, it returns a list of "no change and no action taken." – flyunicorn Aug 24 '20 at 01:25
  • Have you tried closing and opening your terminal(or command prompt for windows)? – harryghgim Aug 24 '20 at 03:25
0

Since you mentioned you use conda you can do something like the following. From your terminal:

conda create -n sb python=3
conda activate sb
conda config --env --add channels conda-forge
conda install -y pandas matplotlib numpy scipy seaborn jupyterlab # some default packages
jupyter lab

Whenever you want to use this conda environment again you have to do

conda activate sb

before you can run jupyter lab.

Note, if you didn't changed the default, you should see your terminal prompt changing when activating an environment, i.e. the name of the environment comes before your prompt. In our case here (sb) <prompt>.

To solve the issue with your current conda environment, more information is needed.

Stefan
  • 1,697
  • 15
  • 31
  • By second line(conda activate sb), it returns: CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. To initialize your shell, run $ conda init Currently supported shells are: - bash - fish - tcsh - xonsh - zsh - powershell See 'conda init --help' for more information and options. IMPORTANT: You may need to close and restart your shell after running 'conda init'. – flyunicorn Aug 23 '20 at 09:16
  • @flyunicorn Have you tried running `conda init `? If not, please do so. – Stefan Aug 23 '20 at 19:03
  • I was able to run conda update conda and conda install seaborn. Both run successfully, but when I import seaborn, same error of no such module. So my notebook is accessing different location for packages other than through conda? – flyunicorn Aug 24 '20 at 00:02
  • I think I know where the mismatch is. After I run pip install seaborn, it returns: Requirement already satisfied: seaborn in /Users/Cynthia/anaconda3/lib/python3.6/site-packages (0.9.0)....After I run sys.path, it shows:'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5'. So it seems seaborn is saved in python 3.5 whereas the path was pointing to python 3.5. How to solve this? – flyunicorn Aug 24 '20 at 01:50
  • @flyunicorn sys.path isn't just one thing. It is a list containing paths. It equals what you see when you run echo $PATH. It can have multiple `Versions`, if you have installed multiple python versions. – harryghgim Aug 24 '20 at 03:28
  • @flyunicorn `conda activate sb` will automatically add the correct path to `sys.path` at the beginning such that this python environment gets found first by your system. If you then launch python it can and will only see / find the packages you have installed in your `sb` environment. – Stefan Aug 24 '20 at 05:48
  • @flyunicorn, are you now able to `activate your environment? I.e. is your shell initialized? – Stefan Aug 24 '20 at 05:49
  • @Stefan activate my environment doesn't work. It showed shell not configured. But when I tried the steps you mentioned, it returns no change or action taken. I tried adding one path to sys.path and finally the import works. – flyunicorn Aug 24 '20 at 09:47
  • @flyunicorn good that it is working now, however you should sort out the problem with your conda installation. From one of your above posts I saw that you installed conda in `/Users/Cynthia/anaconda3`. The conda FAQ specify you should run `source /Users/Cynthia/anaconda3/bin/activate && conda init`, this should modify your shell setup accordingly instead of having to add the default conda path to your `PATH`. – Stefan Aug 24 '20 at 11:11