gpt4 book ai didi

python - Jupyter 实验室安装/导入包

转载 作者:行者123 更新时间:2023-12-02 17:06:43 27 4
gpt4 key购买 nike

我正在尝试为我正在使用的笔记本安装 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 notebook 中检查了 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']

最佳答案

我遇到了同样的问题。对我来说,我在 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)”。

关于python - Jupyter 实验室安装/导入包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51433227/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com