Home | News | Documentation | Download

Cannnot import ROOT in JupyterHub

I’m trying to interface The Littlest Jupyter Hub (TLJH) with ROOT.
Right now I install ROOT compiling it from the source, with the following commands:

sudo apt --yes install python3 python3-dev git curl
sudo apt-get --yes install dpkg-dev cmake g++ gcc binutils libx11-dev libxpm-dev libxft-dev libxext-dev python libssl-dev

git clone --branch v6-22-00-patches https://github.com/root-project/root.git root_src

mkdir builddir installdir

cd builddir

cmake -DCMAKE_INSTALL_PREFIX=../installdir -DPYTHON_EXECUTABLE=/usr/bin/python3 ../root_src

sudo cmake --build . --target install 

Then I add the thisroot.sh source command in the .profile file.
Finally I install TLJH, using

sudo curl -L https://tljh.jupyter.org/bootstrap.py | sudo -E python3 - --admin <admin name>

specifying my admin username.
Everything works fine, I am even able to “import ROOT” from python3 in the terminal.

As soon as I create a new python3 file on JupyterHub, and I try to import ROOT, an error message comes out:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-ee8dc4376aa8> in <module>
----> 1 import ROOT

ModuleNotFoundError: No module named 'ROOT'

I am open to any suggestion.

_ROOT Version:6.22.02
_Platform:Ubuntu 18.04 bionic server


This looks like an environment setting problem. When you say “a new python3 file on Jupyterhub” you mean a notebook?

What I would do is look in the JupyterHub documentation and check how to specify environment variables for the kernels. Then you will need to add the same PYTHONPATH and LD_LIBRARY_PATH that thisroot.sh sets (the fact that you put it in .profile does not seem to have any effect over the kernels of JupyterHub).

(Yes I meant a notebook)
Got it, I’ll try it now. Thanks a lot!

It seems I’ve partially solved the problem by importing PYTHONPATH and LD_LIBRARY_PATH through:

c.Spawner.environment = {
'PYTHONPATH': 'path_in_the_thisroot_file',
'LD_LIBRARY_PATH': 'path_in_the_thisroot_file'

placed into a file which I created and named environment.py placed in /opt/tljh/config/jupyterhub_config.d/ (root privileges requested).

By doing this, the correct PATH is imported into JupyterHub.
Then an additional issue appeared. The python kernel of the notebooks is by default set to py3.7, while I built ROOT with py3.6, so actually I’m not able to import ROOT from the notebook because of this incompatibility.

Additional suggestion are highly appreciated. In the meantime, thanks to @etejedor for the help!


The python kernel of the notebooks is by default set to py3.7, while I built ROOT with py3.6, so actually I’m not able to import ROOT from the notebook because of this incompatibility.

You can try a couple of things. The first one would be to build ROOT with the same Python3.7 installation that Jupyter uses. More details on how to point ROOT to a particular Python installation here:

Another option would be to modify the Jupyter Python3 kernel to point to the Python3.6 installation that ROOT was built against. For that, you can check where the kernel is installed with:

jupyter kernelspec list

And then modify the kernel.json of the Python3 kernel directory to point to your python3.6.

I’m trying to achieve both options, starting from building ROOT with py3.7. I take the interpreter from the installation directory related to TLJH, in order to match the exact version. The message I get when I try to run

cmake -DCMAKE_INSTALL_PREFIX=../installdir -DPYTHON_EXECUTABLE=/opt/tljh/user/bin/python3.7  ../root_src

Citing this thread, I also tried to add the option -DPYTHON_LIBRARY=/opt/tljh/user/lib/python3.7/config-3.7m-x86_64-linux-gnu/libpython3.7m.a and I get the following message:

-- Detected ROOT_VERSION 6.22.99
-- Looking for Python
-- Found PythonLibs: /opt/tljh/user/lib/python3.7/config-3.7m-x86_64-linux-gnu/libpython3.7m.a (found version "3.6.9") 
CMake Error at cmake/modules/SearchRootCoreDeps.cmake:230 (message):
  Version mismatch between Python interpreter (3.7.3) and libraries (3.6.9).

  ROOT cannot work with this configuration.  Please specify only
  PYTHON_EXECUTABLE to CMake with an absolute path to ensure matching
  versions are found.
Call Stack (most recent call first):
  CMakeLists.txt:129 (include)

-- Configuring incomplete, errors occurred!
See also "/home/ubuntu/builddir/CMakeFiles/CMakeOutput.log".

Moreover, no file of the type libpython3.7.so is present.


Specifying PYTHON_EXECUTABLE should be sufficient. You are using ROOT 6.22/02, and what cmake version are you using?

If you only specify PYTHON_EXECUTABLE, you also get the version mismatch error?

Yes, even if I specify only the PYTHON_EXECUTABLE, the message remains the same.
The cmake version is 3.10.2

Ok, then what I suggest is either try option 2 (modify the Jupyter kernel to point to Python3.6) or update your CMake to >=3.14 and try option 1 again.

The file CMakeCache.txt complicated things a lot.
The PYTHON_EXECUTABLE was correct, but the CMakeCache.txt was an old version, not updated. After having eliminated it, everything worked.
Thanks again @etejedor for the help :smiley:

Glad to hear it worked!