在jupyter笔记本上找不到PhantomJS

时间:2018-11-25 04:10:37

标签: python phantomjs systemd

我正在尝试将散景图导出到图像。为此,我使用方法export_png,该方法在下面使用PhantomJS和Selenium。但是,我遇到了RuntimeError

RuntimeError: PhantomJS is not present in PATH or BOKEH_PHANTOMJS_PATH. 
Try "conda install phantomjs" or "npm install -g phantomjs-prebuilt"

可以重现此行为:

笔记本

import shutil
shutil.which('phantomjs') or 'not found'
# --> "not found"

在同一virtualenv中使用IPython

import shutil
shutil.which('phantomjs') or 'not found'
# --> "/home/<user>/miniconda/envs/p36/bin/phantomjs"

[编辑]

笔记本服务器在Linux(Ubuntu)上作为服务运行。我认为问题出在此,因为当我手动启动笔记本服务器时,在笔记本上很好地找到了phantomJS路径。这里是详细的systemd文件:

[Unit]
Description=Jupyter Notebook
After=multi-user.target network.target

[Service]
User=<myuser>
Group=<mygroup>
EnvironmentFile=/etc/environment
Type=idle
Restart=always
RestartSec=3
ExecStart=/home/<user>/miniconda3/envs/tensorflow/bin/jupyter-notebook --no-browser --notebook-dir="/home/<user>/src"

[Install]
WantedBy=multi-user.target

[解决方案]

问题比Python问题更多是系统问题:$PATH使用的环境变量是root用户之一,而不是指定用户之一。在运行服务的脚本之前,我没有找到任何清晰的方法来加载.bashrc文件。然后的解决方案是使用一个环境文件,其中我声明$PATH变量的方式与在.bashrc文件中声明的方式相同。在那之后,一切都像一个沙姆。

1 个答案:

答案 0 :(得分:1)

在systemd中启动进程之前,没有(永远不会有)内置方法来加载指定用户的~/.bashrc

[Nit:如果要在点文件中设置环境变量,请在~/.bash_profile而不是~/.bashrc中进行设置。前者来自所有登录外壳(每次登录大约一次),后者来自每个交互外壳(而不是登录外壳提供,因此DE的根进程将不会获得这些变量)。]

systemd不是bash解释器。 ~/.bashrc可能包含任意bash代码,而不仅仅是环境变量分配,因此systemd无法实现这种功能。

因此,如果您需要运行任意的shell代码,只需明确地执行它即可:

ExecStart=/bin/bash -c '...'