问题描述
我们使用anaconda创建虚拟环境。
conda create -n qq python=3.7 jupyter
conda activate qq
得到虚拟环境前缀(qq),激活成功。
这步是成功了,但是直接运行jupyter notebook的话,并没有使用(qq)环境里的python。
在python中,使用如下命令可以查看
import sys
sys.path
得到
/home/qq/anaconda3/bin/python
可见,使用的还是anaconda的主python环境。
所以import torch,import tensorflow,会报错,提示no module named 'xxx'。
因为anaconda主环境里没装,我们装在了虚拟环境里。
那么,怎么才能让jupyter notebook使用/envs/XXX/环境下的python呢?
解决思路的核心思想是,不改变anaconda和jupyter的具体结构,而是用一些config的小手段来解决。
1.用anaconda内生插件解决问题
conda install nb_conda
安装完这个牛逼conda之后,再次启动jupyter notebook,就能看到所有虚拟环境都显示出来了。
//参考https://www.jianshu.com/p/afea092dda1d
2.用jupyter插件解决问题
激活环境后,安装ipykernel
conda activate qq
conda install ipykernel
安装好之后,运行如下命令。
python -m ipykernel install --name {你的虚拟环境名字,如qq} --display-name {你想显示的名称}
这个方法,本质上跟方法1是一样的。
原理上来讲,都是在 ......../jupyter/kernels/目录下,创建一个命名为{对应名称}的文件夹,文件夹下放一个kernel.json文件。
于是我们得到....../jupyter/kernels/{对应名称}/kernel.json
运行python -m ipykernel .... 那行命令,就是做创建文件夹和创建文件这两件事情。
在没运行python -m ipykernel ....之前,用find找一下这个文件
find . -name 'kernel.json'
可以看到输出为
./anaconda3/pkgs/ipykernel-5.1.0-py37h39e3cac_0/share/jupyter/kernels/python3/kernel.json
./anaconda3/share/jupyter/kernels/python3/kernel.json
这个/share/jupyter/kernels/python3/kernel.json就是关键了。
我们来看看json文件的内容
{
"argv": [
"/home/qq/anaconda3/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"language": "python"
}
正是因为argv中设置的地址为'./anaconda3/bin/python',才导致jupyter的默认kernel为anaconda的主环境。
3.小结
显然,
如果我们在对应的/kernels/目录下,新建一个文件夹,再新建一个kernel.json,
把地址设置为'./anaconda3/envs/qq/bin/python',
就可以用到虚拟环境qq下的python了。
无论是 nb_conda,还是ipykernel,本质上都是在做这件事情。
只不过一个是用anaconda的插件,一个是用jupyter的插件。
对软件实体并没有改变,是为气宗。
//参考https://www.jianshu.com/p/f70ea020e6f9
————————————————
版权声明:本文为CSDN博主「w55100」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/w55100/article/details/88925697
方法二:
如何在 notebook 上激活本地虚拟环境,部署完成后可以选择本地的虚拟环境conda
首先激活对应的conda环境
source activate bf-predict-env
安装 ipykernel
conda install --name bf-predict-env ipykernel
将环境写入notebook的kernel中
(bf-predict-env) localhost:prophet-master user$ python -m ipykernel install --user --name bf-predict-env --display-name ‘Python bf-predict-env’
上述操作完成后会报错, ImportError: No Module Named ‘pysqlite2’,执行
conda install --name bf-predict-env sqlite
conda install --name bf-predict-env python=3.6
启动notebook 即可
jupyter notebook
localhost 未发送任何数据。
断开网络加速,不能翻墙,否则浏览器页面会报此错误
install fastai (包含 pytorch)
conda install -c pytorch -c fastai fastai pytorch
在conda 环境,可以切换成 pip 安装源码包,
pip install -e .
安装sklearn
conda install scikit-learn
安装 bert-serving 服务
https://pypi.org/project/bert-serving-server/
conda install tensorflow
pip install bert-serving-server # server
pip install bert-serving-client # client, independent of bert-serving-server
启动 jupyter notebook 时报错, ImportError: cannot import name ‘ensure_dir_exists’. You need to update jupyter_core, and jupyter_client
conda update jupyter_core jupyter_client
To install tensorflow-hub package with conda run one of the following:
conda install -c conda-forge tensorflow-hub
conda install -c conda-forge/label/gcc7 tensorflow-hub
conda install -c conda-forge/label/cf201901 tensorflow-hub
在 notebook 中安装 包,报错 PermissionError, 后面加 --user jupyter
pip install --user jupyter works like a charm. Thanks.
!pip install bert-tensorflow --user jupyter