Not able to use PyROOT in Python3


ROOT Version: 6.10/04
OS: Linux Mint Tricia (based on Ubuntu 18.04)
Compiler: gcc(and g++) 7.5.0
Python2 version: Python 2.7.17
Python3 version: Python 3.6.9


I’m a user of ROOT and recently I started doing a course based on JupyterNotebooks and Root.

I’m currently not able to import ROOT inside a Jupyter Notebook. This is the message I receive after running “import ROOT”:

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

~/HEPSoftwares/ROOT/root-build/lib/ROOT.py in <module>
     22 ### system and interpreter setup ------------------------------------------------
     23 import os, sys, types
---> 24 import cppyy
     25 
     26 

~/HEPSoftwares/ROOT/root-build/lib/cppyy.py in <module>
     59       sys.setdlopenflags( 0x100 | 0x2 )    # RTLD_GLOBAL | RTLD_NOW
     60 
---> 61    import libPyROOT as _backend
     62 
     63    # reset dl flags if needed

ImportError: dynamic module does not define module export function (PyInit_libPyROOT)

I’ve made some tests and discovered that I can import ROOT in Python2 terminal but the same it’s not true for a Python3 terminal:

Python2:

ecaue@TARDIS:~$ python2
Python 2.7.17 (default, Feb 27 2021, 15:10:58) 
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
>>> 

Python3:

ecaue@TARDIS:~$ python3
Python 3.6.9 (default, Dec  8 2021, 21:08:43) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ecaue/HEPSoftwares/ROOT/root-build/lib/ROOT.py", line 24, in <module>
    import cppyy
  File "/home/ecaue/HEPSoftwares/ROOT/root-build/lib/cppyy.py", line 61, in <module>
    import libPyROOT as _backend
ImportError: dynamic module does not define module export function (PyInit_libPyROOT)
>>> 

I’m using an old ROOT distribution because I got some issues installing the recent ones. It is installed inside the dir located in

/home/ecaue/HEPSoftwares/ROOT/root-build

Jupyter was installed using “pip3 install jupyter”.

This is how my environment variables are defined in my .bashrc file:

source /home/ecaue/HEPSoftwares/ROOT/root-build/bin/thisroot.sh
export ROOTSYS=/home/ecaue/HEPSoftwares/ROOT/root-build
export PATH=$ROOTSYS/bin:$PATH
export LD_LIBRARY_PATH=$ROOTSYS/lib/:$LD_LIBRARY_PATH

I really appreciate if someone can help me find out how to force PyRoot to run in Jupyter since I installed it using python3.

Hello,

It seems the ROOT installation you are using was built for Python2, so you need to make sure you run your Jupyter notebook with a Python2 kernel, not Python3.

Alternatively, I would recommend upgrading ROOT. An easy way to install a recent ROOT is conda

You’ll be able to use ROOT+Python3+Jupyter in a conda environment.

Hi, Etejedor.

Your idea of upgrading ROOT seems really good! Sadly I have to use Python3 since the course I’m doing was not made for Python2.

I created a conda environment using this link:

$ conda config --set channel_priority strict
$ conda create -c conda-forge --name RootEnv root
$ conda activate RootEnv

But I’m still getting an error message:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Input In [1], in <module>
----> 1 import ROOT

File ~/HEPSoftwares/ROOT/root-build/lib/ROOT.py:24, in <module>
     22 ### system and interpreter setup ------------------------------------------------
     23 import os, sys, types
---> 24 import cppyy
     27 ## there's no version_info in 1.5.2
     28 if sys.version[0:3] < '2.2':

File ~/HEPSoftwares/ROOT/root-build/lib/cppyy.py:61, in <module>
     58    dlflags = sys.getdlopenflags()
     59    sys.setdlopenflags( 0x100 | 0x2 )    # RTLD_GLOBAL | RTLD_NOW
---> 61 import libPyROOT as _backend
     63 # reset dl flags if needed
     64 if needsGlobal:

ImportError: dynamic module does not define module export function (PyInit_libPyROOT)

Hi Caue, you might like to try the snap build of ROOT, on Linux Mint 19, installation should be

sudo apt install snapd -y
sudo snap install snapd

You’ll need to reboot your system to have the proper environment set-up, and can then issue the following

sudo snap install root-framework
root --notebook

Any other versions of ROOT should exist side by side just fine, however if you’ve done anything similar to placing ./thisRoot.sh in your .bashrc, you’ll need to consider removing it so that the snap installation is available. There’s no equivilent to Conda activate or ./thisRoot.sh for the snap build, but using either of those two would make them have higher priority than the snap, which is how they can co-exist side by side.

For potential future readers on LM 20 or newer, you’ll need to run

sudo rm /etc/apt/preferences.d/nosnap.pref
sudo apt update

Prior to running the above commands.

Hi, James!

I removed from my .bashrc the lines concerning ROOT, and re-installed it using snap and
it worked! Thank you!

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