Jupyter实验室安装/导入软件包

时间:2018-07-19 23:50:39

标签: python anaconda jupyter-notebook

我正在尝试为正在使用的笔记本计算机安装plotnine。我已完成以下操作:

  1. 使用python 3.6创建了一个conda环境,并添加了plotnine

  2. 在激活了上述环境的情况下启动jupyter lab

  3. 在笔记本中,我添加了以下行: !conda install -c conda-forge --yes plotnine

但是,我的输出没有意义。首先说所有请求的软件包都已安装,然后说找不到模块

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

如果存在已知冲突,请参见完整的导入语句:

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

编辑:我还检查了jupyter笔记本中的sys.path,并获得了以下内容。我在这里看不到有关conda的任何信息。我应该更新PATH还是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 个答案:

答案 0 :(得分:1)

我有同样的问题。在我看来,我在Jupyter Lab中的笔记本正在运行基本内核,而不是虚拟环境的内核。键入

import sys
sys.executable

插入笔记本。对我来说,我得到了结果

'/anaconda3/bin/python'

而不是期望的

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

我按照iPython documentation中的说明解决了这个问题。总之,您需要为新环境安装新的iPython内核。运行:

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

然后,在新环境中运行Jupyter Lab:

conda activate myenv
jupyter lab

打开新笔记本时(也位于现有笔记本的右上角),您应该能够选择内核“ Python(myenv)”。