简体   繁体   中英

Jupyter lab installing/importing packages

I am trying to install plotnine for a notebook I am using. I have done the following:

  1. Created a conda environment using python 3.6, and adding plotnine

  2. Launching jupyter lab with the above environment activated

  3. In the notebook, I added the following line: !conda install -c conda-forge --yes plotnine

However, my output makes no sense. First it says that all requested packages have been installed, and then it says it cannot find the module

!conda install -c conda-forge --yes plotnine
from plotnine import *

Solving environment: done

# All requested packages already installed.

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-386ef81e08ff> in <module>()
     11 get_ipython().system('conda install -c conda-forge --yes plotnine')
     12 ######
---> 13 from plotnine import *     # python clone of ggplot2
     14 matplotlib.rcParams['figure.figsize'] = [12, 8]
     15 matplotlib.rcParams['lines.linewidth'] = 2

ImportError: No module named 'plotnine'

In case there is a known conflict, here is the entire import statement:

import gsc # proprietary module
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
from ipywidgets import interact, FloatSlider
from util_demo import *
# adding installation of plotnine, which is not included by default
# import sys
!conda install -c conda-forge --yes plotnine
######
from plotnine import *     # python clone of ggplot2
matplotlib.rcParams['figure.figsize'] = [12, 8]
matplotlib.rcParams['lines.linewidth'] = 2
matplotlib.rcParams['xtick.labelsize'] = 24
matplotlib.rcParams['ytick.labelsize'] = 24
matplotlib.rcParams['legend.fontsize'] = 24
matplotlib.rcParams['axes.labelsize'] = 24

EDIT : I also checked sys.path within the jupyter notebook and get the following. I do not see anything about conda here. Should I update either PATH or PYTHONPATH ?

['',
 '/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python35.zip',
 '/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5',
 '/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin',
 '/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload',
 '/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages',
 '/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/IPython/extensions',
 '/Users/adamg/.ipython']

I had the same problem. It looks like for me, my notebook in Jupyter Lab was running the base kernel and not the kernel of the virtual environment. Type

import sys
sys.executable

into your notebook. For me, I got the result

'/anaconda3/bin/python'

instead of the desired

'/anaconda3/envs/myenv/bin/python'

I solved it by following the instructions in the iPython documentation . In summary, you need to install a new iPython kernel for your new environment. Run:

conda install -n myenv ipython
conda activate myenv
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"

Then, to run Jupyter Lab in the new environment:

conda activate myenv
jupyter lab

And you should be able to select the kernel "Python (myenv)" when you open a new notebook (also in the top right of an existing notebook).

#try pip install first and rerun; the code line below should solve it:

pip install plotnine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM