Importing ROOT in Jupyter Notebook with Python3

Hello!

I installed ROOT from source exactly as indicated in ROOT install. It is installed in /usr/local/ inside the new folder root_install.

I can successfully run ROOT in the terminal using root and can work there.

I also installed python with Anaconda:

$ python --version
Python 3.7.6

The problem is the following: I can import the ROOT package in Python in terminal

$ python
Python 3.7.6 (default, Jan  8 2020, 13:42:34) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
>>>

and also in any text editor (Sublime Text 3 for example), but I cannot import ROOT in Jupyter Notebook:

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

/usr/local/root_install/lib/ROOT/__init__.py in <module>
     89     ip = get_ipython()
     90     if hasattr(ip,"kernel"):
---> 91         import JupyROOT
     92         import JsMVA
     93 

/usr/local/root_install/lib/ROOT/_facade.py in _importhook(name, *args, **kwds)
    119                 except Exception:
    120                     pass
--> 121             return _orig_ihook(name, *args, **kwds)
    122         __builtin__.__import__ = _importhook
    123 

/usr/local/root_install/lib/JupyROOT/__init__.py in <module>
----> 1 from JupyROOT.helpers import cppcompleter, utils
      2 
      3 if '__IPYTHON__' in __builtins__ and __IPYTHON__:
      4     cppcompleter.load_ipython_extension(get_ipython())
      5     utils.iPythonize()

/usr/local/root_install/lib/ROOT/_facade.py in _importhook(name, *args, **kwds)
    119                 except Exception:
    120                     pass
--> 121             return _orig_ihook(name, *args, **kwds)
    122         __builtin__.__import__ = _importhook
    123 

/usr/local/root_install/lib/JupyROOT/helpers/cppcompleter.py in <module>
      6 #-----------------------------------------------------------------------------
      7 
----> 8 from JupyROOT.helpers import utils
      9 import ROOT
     10 

/usr/local/root_install/lib/ROOT/_facade.py in _importhook(name, *args, **kwds)
    119                 except Exception:
    120                     pass
--> 121             return _orig_ihook(name, *args, **kwds)
    122         __builtin__.__import__ = _importhook
    123 

/usr/local/root_install/lib/JupyROOT/helpers/utils.py in <module>
     25 import IPython.display
     26 import ROOT
---> 27 from JupyROOT.helpers import handlers
     28 
     29 # We want iPython to take over the graphics

/usr/local/root_install/lib/ROOT/_facade.py in _importhook(name, *args, **kwds)
    119                 except Exception:
    120                     pass
--> 121             return _orig_ihook(name, *args, **kwds)
    122         __builtin__.__import__ = _importhook
    123 

/usr/local/root_install/lib/JupyROOT/helpers/handlers.py in <module>
     24 major, minor = sys.version_info[0:2]
     25 libjupyroot_mod_name = 'libJupyROOT{}_{}'.format(major, minor)
---> 26 _lib = importlib.import_module(libjupyroot_mod_name)
     27 
     28 

~/opt/anaconda3/lib/python3.7/importlib/__init__.py in import_module(name, package)
    125                 break
    126             level += 1
--> 127     return _bootstrap._gcd_import(name[level:], package, level)
    128 
    129 

ImportError: dlopen(/usr/local/root_install/lib/libJupyROOT3_7.so, 2): Library not loaded: @rpath/Python3.framework/Versions/3.7/Python3
  Referenced from: /usr/local/root_install/lib/libJupyROOT3_7.so
  Reason: image not found

I also tried with root --notebook and importing ROOT with the Python3 kernel, but the same error appears.

I tried for a solution following this answer (changing ldd to otool -L for MacOS, and chaging libPyROOT.so to libJupyROOT3_7.so, even though I don’t know if that makes sense):

$ otool -L libJupyROOT3_7.so | grep python
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/objdump: error: 'libJupyROOT3_7.so': No such file or directory

I have also searched for the file libJupyROOT3_7.so inside /usr/local/root_install/lib/ and it is there.

5 days ago I tried installing ROOT with Homebrew but then I couldn’t even import ROOT succesfully in terminal with Python.

I am very new with all these things so if you can help me it would be very much appreciated.

Thank you very much.


ROOT Version: 6.22/01
Platform: MacOS Catalina 10.15.4
Compiler: Not Provided


1 Like

Maybe @etejedor can help you with this issue

Hello,

If ROOT is installed in /usr/local/root_install/ , you need to run:
source /usr/local/root_install/bin/thisroot.sh

This will set the environment necessary for PyROOT (both from the Python terminal and from the notebook) to find the ROOT Python module.

Hello and thank you for your response!

I’ve already done that too, right after the installation of ROOT.

Francisco

When libJupyROOT3_7.so is built it is linked to libpython, so it should be found.

The result of your otool -L command points to some error generating libJupyROOT, did you observe any error during the ROOT build?

@oshadura might be familiar with the error?

Hello

No, I don’t remember seeing any error during compilation. May I need to compile everything again? If so, how can I uninstall ROOT if it was installed by source? Simply removing the folder /usr/local/root_install ?

Thank you very much!

Yes, by removing the /usr/local/root_install directory you will remove the installation.

I would build ROOT from scratch again, and please post here the configuration messages where it tells you with what Python version it is building ( PyROOT will be built for ...) and any possible errors/warnings around the message Built target JupyROOT.

Thank you,
Enric

1 Like

Hello Enric,

I tried building again ROOT from source.
Since I have Python installed in

$ which python
/Users/francisco/opt/anaconda3/bin/python

I run

$ sudo cmake -DCMAKE_INSTALL_PREFIX=../root_install -DPython3_EXECUTABLE=/Users/francisco/opt/anaconda3/bin/python3 ../root_src

and the following message regarding Python appears (nothing’s wrong):

-- Looking for Python
-- Found Python3: /Users/francisco/opt/anaconda3/bin/python3 (found version "3.7.6") found components: Interpreter Development NumPy 
-- Found Python2: /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 (found version "2.7.16") found components: Interpreter Development NumPy 
-- ROOT default compression algorithm: zlib
-- PyROOT will be built for versions 3.7.6 (Main) and 2.7.16

As a side note, reading the output I found that neither Pythia6 or Pythia8 was builded:

-- Looking for Pythia6
-- Pythia6 not found. Switching off pythia6 option
-- Looking for Pythia8
-- Could NOT find Pythia8 (missing: PYTHIA8_INCLUDE_DIR PYTHIA8_LIBRARY) 
-- Pythia8 not found. Switching off pythia8 option 

Then, I run
$ sudo cmake --build . -- install -j6
and no error messages appears.

Now I run python in command line and, as before, no error:

Python 3.7.6 (default, Jan  8 2020, 13:42:34) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
>>>

However, in a Python Kernel jupyter notebook (started using jupyter notebook), when I try to import ROOT now the Kernel crashes and the notebook dies. The error in terminal is very long, but the only line mentioning ROOT is the following:
[/usr/local/root_install/lib/libCore.so] TUnixSystem::DispatchSignals(ESignals) (no debug info)

If you need any more information, ask me.

Thank you very much for your time,

Francisco

Hi Francisco,

I think now you have hit this issue with JupyROOT and conda:

https://sft.its.cern.ch/jira/browse/ROOT-10884

We still need to investigate it, so in the meantime what I would do is compile ROOT with a Python that is not the conda one, for now. Actually, you also built with

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7

what happens if you use a Python2 kernel instead of Python3? Does Jupyter work?

1 Like

Dear Enric,

For what is worth, I could import ROOT with IPython in console doing this:

$ ipython
Python 3.7.6 (default, Jan  8 2020, 13:42:34) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import ROOT                                                                                               

In [2]: ROOT                                                                                                      
Out[2]: <module 'ROOT' from '/usr/local/root_install/lib/ROOT/__init__.py'>

I will certainly try that later!

Thank you very much!

Francisco

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.