Jupyter lab installing/importing packages

风格不统一 提交于 2020-01-06 05:34:11

问题


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']

回答1:


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).



来源:https://stackoverflow.com/questions/51433227/jupyter-lab-installing-importing-packages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!