本文最后更新于 944 天前,其中的信息可能已经有所发展或是发生改变。
问题总结
如果有个库或者其他依赖在 bash
的 python
和 ipython
中可以正常使用,但是在 jupyter(jupyter notebook, jupyterlab, jupyterhub)
中无法使用。那其问题是由于 jupyter 中的环境变量未与 bash 环境变量 同步导致的。具体问题和解决方案见 github jupyter/notebook issue1290: library path not work in notebook。
解决方案总结如下:
jupyter
中执行%env
,会列出 jupyter 中的环境变量bash
中执行printenv
,会列出 bash下的环境变量- 比较上面两组环境变量,记下 jupyter 中缺失的项以及值,编辑 jupyter 的配置文件,将缺失的环境变量以及值加入到配置文件里
jupyter 配置文件: .jupyter/jupyter_notebook_config.py
在配置文件前加:
import os c = get_config() os.environ['PATH_NAME'] = 'PATH_VALUE' c.Spawner.env.update('PATH_NAME')
配置示范
在树莓派中使用 NSC2 ,配置时 OpenVINO ,由于没有显示屏,所有程序的调试和结果的显示全部在 jupyterlab 中完成。在安装完 OpenVINO 后发现 jupyterlab 中无法导入 cv2,提示 libopencv_ml.so.4.0: cannot open shared object file: No such file or directory ,然后我检查了 bashrc 文件,发现正常,而且在 bash 中可以正确导入 cv2。这就说明 jupyterlab 的环境并没有与 bash 中配置同步过来。
检查 jupyterlab 的环境变量:
检查 bash 环境变量:
发现 jupyterlab 环境变量中缺少了:
于是将其加入到 jupyter_notebook_config 中就好了
Greetings! Very useful advice in this particular post!
It is the little changes which will make the largest changes.
Thanks a lot for sharing!| I absolutely love your website..
Pleasant colors & theme. Did you build this web site
yourself? Please reply back as I?m wanting to create
my own blog and would like to find out where you got this from or what the theme
is named.
Appreciate it! –https://clin–***
Sorry for revise your comment, cause orginal comment has some Suspicious url. My website theme called No.7, and I added some extra js: mouse move, mouse click, type write, live2D, … cann’t remember any more.