Notebook: Imports from notebook use wrong env

Created on 18 Dec 2017  路  7Comments  路  Source: jupyter/notebook

I have two conda envs, dev and gis. Opening a notebook under the gis env attempts to import certain libraries from the dev env. Note that when I use the IPython or Python console, the correct env is used, and import occurs without error.

I am doing:

>>> source activate gis
>>> jupyter notebook

Then, when I try to import, say, the geopandas library, I get:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-5-c9ee6bd24dbc> in <module>()
----> 1 import geopandas

~/anaconda3/envs/dev/lib/python3.6/site-packages/geopandas/__init__.py in <module>()
      2 from geopandas.geodataframe import GeoDataFrame
      3 
----> 4 from geopandas.io.file import read_file
      5 from geopandas.io.sql import read_postgis
      6 from geopandas.tools import sjoin

~/anaconda3/envs/dev/lib/python3.6/site-packages/geopandas/io/file.py in <module>()
      1 import os
      2 
----> 3 import fiona
      4 import numpy as np
      5 

ModuleNotFoundError: No module named 'fiona'

Notice the env lib that is being referenced is dev. If I list the environment variables from within a notebook, I see 'CONDA_DEFAULT_ENV': 'gis', so it appears to be set up correctly.

Here is the list of packages in the gis env:

# packages in environment at /Users/fonnescj/anaconda3/envs/gis:
#
appnope                   0.1.0                    py36_0    conda-forge
backports                 1.0                      py36_1    conda-forge
backports.functools_lru_cache 1.4                      py36_1    conda-forge
bleach                    2.0.0                    py36_0    conda-forge
ca-certificates           2017.11.5                     0    conda-forge
certifi                   2017.11.5                py36_0    conda-forge
clangdev                  5.0.0                 default_0    conda-forge
click                     6.7                      py36_0    conda-forge
click-plugins             1.0.3                    py36_0    conda-forge
cligj                     0.4.0                    py36_0    conda-forge
curl                      7.55.1                        0    conda-forge
cycler                    0.10.0                   py36_0    conda-forge
decorator                 4.1.2                    py36_0    conda-forge
descartes                 1.1.0                    py36_0    conda-forge
entrypoints               0.2.3                    py36_1    conda-forge
expat                     2.1.0                         3    conda-forge
fiona                     1.7.11                   py36_0    conda-forge
freetype                  2.7                           2    conda-forge
freexl                    1.0.4                         0    conda-forge
gdal                      2.1.3                    py36_7    conda-forge
geopandas                 0.3.0                    py36_0    conda-forge
geos                      3.6.2                         1    conda-forge
giflib                    5.1.4                         0    conda-forge
hdf4                      4.2.13                        0    conda-forge
hdf5                      1.10.1                        1    conda-forge
html5lib                  1.0.1                      py_0    conda-forge
icu                       58.2                          0    conda-forge
intel-openmp              2018.0.0             h8158457_8  
ipykernel                 4.7.0                    py36_0    conda-forge
ipyparallel               6.0.2                    py36_0    conda-forge
ipython                   6.2.1                    py36_0    conda-forge
ipython_genutils          0.2.0                    py36_0    conda-forge
ipywidgets                7.0.5                    py36_0    conda-forge
jedi                      0.10.2                   py36_0    conda-forge
jinja2                    2.10                     py36_0    conda-forge
jpeg                      9b                            2    conda-forge
json-c                    0.12.1                        0    conda-forge
jsonschema                2.6.0                    py36_0    conda-forge
jupyter                   1.0.0                    py36_0    conda-forge
jupyter_client            5.1.0                    py36_0    conda-forge
jupyter_console           5.2.0                    py36_0    conda-forge
jupyter_core              4.4.0                      py_0    conda-forge
kealib                    1.4.7                         4    conda-forge
krb5                      1.14.2                        0    conda-forge
libcxx                    5.0.0                         0    conda-forge
libdap4                   3.18.3                        2    conda-forge
libgfortran               3.0.1                h93005f0_2  
libiconv                  1.15                          0    conda-forge
libnetcdf                 4.4.1.1                      10    conda-forge
libpng                    1.6.28                        2    conda-forge
libpq                     9.6.3                         0    conda-forge
libsodium                 1.0.15                        1    conda-forge
libspatialindex           1.8.5                         1    conda-forge
libspatialite             4.3.0a                       18    conda-forge
libssh2                   1.8.0                         2    conda-forge
libtiff                   4.0.9                         0    conda-forge
libxml2                   2.9.5                         2    conda-forge
llvmdev                   5.0.0                 default_0    conda-forge
markupsafe                1.0                      py36_0    conda-forge
matplotlib                2.1.1                    py36_0    conda-forge
mistune                   0.8.3                      py_0    conda-forge
mkl                       2018.0.1             hfbd8650_4  
munch                     2.2.0                    py36_0    conda-forge
nbconvert                 5.3.1                      py_1    conda-forge
nbformat                  4.4.0                    py36_0    conda-forge
ncurses                   5.9                          10    conda-forge
notebook                  5.2.2                    py36_1    conda-forge
numpy                     1.13.3           py36h2cdce51_0  
openjpeg                  2.1.2                         2    conda-forge
openssl                   1.0.2n                        0    conda-forge
pandas                    0.21.1                   py36_0    conda-forge
pandoc                    2.0.5                         0    conda-forge
pandocfilters             1.4.1                    py36_0    conda-forge
patsy                     0.4.1                    py36_0    conda-forge
pcre                      8.39                          0    conda-forge
pexpect                   4.3.0                    py36_0    conda-forge
pickleshare               0.7.4                    py36_0    conda-forge
pip                       9.0.1                    py36_0    conda-forge
proj4                     4.9.3                         5    conda-forge
prompt_toolkit            1.0.15                   py36_0    conda-forge
psycopg2                  2.7.3.2                  py36_0    conda-forge
ptyprocess                0.5.2                    py36_0    conda-forge
pygments                  2.2.0                    py36_0    conda-forge
pyparsing                 2.2.0                    py36_0    conda-forge
pyproj                    1.9.5.1                  py36_0    conda-forge
pyqt                      5.6.0                    py36_4    conda-forge
pysal                     1.14.3                   py36_0    conda-forge
python                    3.6.3                         1    conda-forge
python-dateutil           2.6.1                    py36_0    conda-forge
pytz                      2017.3                     py_2    conda-forge
pyzmq                     16.0.2                   py36_2    conda-forge
qt                        5.6.2                h9e3eb04_4    conda-forge
qtconsole                 4.3.1                    py36_0    conda-forge
readline                  6.2                           0    conda-forge
rtree                     0.8.3                    py36_0    conda-forge
scipy                     1.0.0            py36h1de22e9_0  
seaborn                   0.8.1                    py36_0    conda-forge
setuptools                38.2.4                   py36_0    conda-forge
shapely                   1.6.3                    py36_0    conda-forge
simplegeneric             0.8.1                    py36_0    conda-forge
sip                       4.18                     py36_1    conda-forge
six                       1.11.0                   py36_1    conda-forge
sqlalchemy                1.1.13                   py36_0    conda-forge
sqlite                    3.13.0                        1    conda-forge
statsmodels               0.8.0                    py36_0    conda-forge
terminado                 0.8.1                    py36_0    conda-forge
testpath                  0.3.1                    py36_0    conda-forge
tk                        8.5.19                        2    conda-forge
tornado                   4.5.2                    py36_0    conda-forge
traitlets                 4.3.2                    py36_0    conda-forge
wcwidth                   0.1.7                    py36_0    conda-forge
webencodings              0.5                      py36_0    conda-forge
wheel                     0.30.0                     py_1    conda-forge
widgetsnbextension        3.0.8                    py36_0    conda-forge
xerces-c                  3.2.0                         0    conda-forge
xz                        5.2.3                         0    conda-forge
zeromq                    4.2.1                         1    conda-forge
zlib                      1.2.11                        0    conda-forge

Running Python 3.6 on macOS 10.13.3

Most helpful comment

Check the contents of /Users/fonnescj/Library/Jupyter/kernels/python3 - it's probably pointing to your dev env. If you remove that folder, it should find a default python3 kernel in the same env that you run Jupyter from.

All 7 comments

Check sys.executable to see which environment your kernel is running in, and jupyter kernelspec list to see what kernels it knows about.

It says that I am on the dev env

'/Users/fonnescj/anaconda3/envs/dev/bin/python'

though my command prompt says otherwise.

(gis) fonnescj on ott.dhcp.mc.vanderbilt.edu in ~

It knows about the following kernels:

$ jupyter kernelspec list
Available kernels:
  ir           /Users/fonnescj/Library/Jupyter/kernels/ir
  julia-0.6    /Users/fonnescj/Library/Jupyter/kernels/julia-0.6
  python3      /Users/fonnescj/Library/Jupyter/kernels/python3

The env I am attempting to use was created by:

conda create -n gis -c conda-forge fiona geopandas shapely jupyter notebook ipython

Again, this only occurs from the notebook, not from IPython run in the terminal.

Check the contents of /Users/fonnescj/Library/Jupyter/kernels/python3 - it's probably pointing to your dev env. If you remove that folder, it should find a default python3 kernel in the same env that you run Jupyter from.

Sneaky. Not sure how that got there.

Thanks, @takluyver!

I'm face the same problem, did you got it?it looks like jupyter notebook loading incorrectly environment.

Check if you have installed jupyter in the same environment you are running on. If it's installed in a different environment jupyter will pick the python interpreter from that environment.

Thanks @dranzerashi that was precisely my situation!

Was this page helpful?
0 / 5 - 0 ratings