GuillaumeA
GuillaumeA

Reputation: 3545

PhantomJS not found on jupyter notebook

I'm trying to export a Bokeh plot to an image. For that, I use the method export_png which uses PhantomJS and Selenium underneathh. However, I got a RuntimeError

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

This behavior can be reproduced :

Notebook

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

With IPython in the same virtualenv

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

[Edit]

The notebook server runs as a service on Linux (Ubuntu). I think the issue come from this because when I manually launch the notebook server, phantomJS path is well found on the notebook. Here the detailed systemd file:

[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

[Solution]

The problem is more a systemd issue than a Python one: $PATH environment variable used is the one of the root user, not the one of the specified user. I did not found any clear way to load the .bashrc file before running the script of the service. The solution was then to use an environment file wherein I declared the $PATH variable the same way it is done in the .bashrc file. Everything works like a sharm after that.

Upvotes: 1

Views: 1074

Answers (1)

intelfx
intelfx

Reputation: 2764

There is no (and never will be any) built-in way to load ~/.bashrc of a specified user prior to starting a process in systemd.

[Nit: if you are setting an environment variable in your dotfiles, do it in ~/.bash_profile, not ~/.bashrc. The former is sourced by all login shells (rougly one time per login), the latter is sourced by every interactive shell (and is not sourced by the login shells, so your DE's root processes won't get these variables).]

systemd is not a bash interpreter. ~/.bashrc may contain arbitrary bash code, not just environment variable assignments, therefore there is no way for systemd to implement such functionality.

So, if you need to run arbitrary shell code, simply do it explicitly:

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

Upvotes: 1

Related Questions