RootPy installation - ROOT cannot be imported?

Hi,
this is really a question for the RootPy developers (RootPy is developed by third parties), but at the webpage you link it does state that ROOT (with PyROOT support, which is enabled by default in ROOT) is a requirement for RootPy; so yes, you need to install ROOT (with PyROOT enabled) in order to use RootPy.

Cheers,
Enrico

Hi Enrico,

thanks for your response. I already have ROOT 6.14.00 installed and it’s working. If PyROOT is enabled by default, then this shouldn’t cause any issues either. What does it mean than “ROOT cannot be imported”? Maybe I have to set a certain environment variable?

These lines I put into my .bashrc:

export PATH=$PATH:/localscratch/username/root-v6-14-00/
export PATH=$PATH:/localscratch/username/root-v6-14-00/bin/
export PATH=$PATH:/localscratch/username/root-v6-14-00/lib/
export PYTHONPATH=$PYTHONPATH:/localscratch/username/root-v6-14-00/
export PYTHONPATH=$PYTHONPATH:/localscratch/username/root-v6-14-00/bin/
export PYTHONPATH=$PYTHONPATH:/localscratch/username/root-v6-14-00/lib/

I wasn’t sure which one is required, so I entered different combinations, hope this is fine…

Cheers,
Hyun-Keun

The installer of rootpy (i.e. pip) probably needs to find ROOT when import ROOT is called from their own modules, so just adding to the PYTHONPATH in your bashrc may not be enough depending on how you are installing it. Can you run python and then import ROOT and it’s found?

Hi,

importing in Python 3.5.3 doesn’t work:

>>> import ROOT
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/localscratch/user/root-v6-14-00/lib/ROOT.py", line 24, in <module>
import cppyy
File "/localscratch/user/root-v6-14-00/lib/cppyy.py", line 61, in <module>
import libPyROOT as _backend
ImportError: dynamic module does not define module export function (PyInit_libPyROOT)

…but in Python 2.7.13 it does! So this might have to do something with missing support for Python 3?

Indeed: the init function is called initlib<module> in p2, PyInit_lib<module> in p3. The two have many changes and are not compatible at all. You will need to enable Python3 when building. Before you do, you can save the existing libPyROOT.so somewhere else and point PYTHONPATH to it for p2, so as to support both.

Do I have to rebuild ROOT and use the flags -Dpython=ON and -Dpython3=ON as described here?

Hi @hkl,
That would give you a ROOT installation that supports both python 2 and 3.
If the diagnosis of your problem in this thread is correct, that should indeed solve your issue.

Cheers,
Enrico

Okay, I will try this. Thank you guys!

Hi,

I’ve reinstalled/rebuilt the latest ROOT 6.14.04 doing

sudo cmake -Dpython=ON -Dpython3=ON /<path-to-ROOT-source>
sudo cmake --build .

and added the following to the .bashrc

export PATH=$PATH:/<path-to-ROOT>/
export PATH=$PATH:/<path-to-ROOT>/bin/
export PATH=$PATH:/<path-to-ROOT>/lib/
export PYTHONPATH=$PYTHONPATH:/<path-to-ROOT>/
export PYTHONPATH=$PYTHONPATH:/<path-to-ROOT>/bin/
export PYTHONPATH=$PYTHONPATH:/<path-to-ROOT>/lib/

Unfortunately, the problem persists and doing sudo pip install rootpy throws the same error. *argh* Am I missing something?

Cheers,
Hyun-Keun

There is no -Dpython3=ON option in ROOT anymore. What you probably want to do is to use -DPYTHON_EXECUTABLE=$(which python) or use the full path to the python you want to compile ROOT for. Currently, there is no way to build ROOT with support for Python 2 and 3 simultaneously.

I’ve recompiled forcing DPYTHON_EXECUTABLE=$/usr/bin/python3.5, but the problem persists…ooff.

This tutorial (bottom of the page) gives additional lines to be written to .bashrc, but they are not mentioned in the official ROOT documentation:

export ROOTSYS=/usr/local/root
export PATH=$ROOTSYS/bin:$PATH
export PYTHONDIR=$ROOTSYS
export LD_LIBRARY_PATH=$ROOTSYS/lib:$PYTHONDIR/lib:$ROOTSYS/bindings/pyroot:$LD_LIBRARY_PATH
export PYTHONPATH=$ROOTSYS/lib:$PYTHONPATH:$ROOTSYS/bindings/pyroot

Are these required?

Are these required?

They do something equivalent to source thisroot.sh, which I assume you were already doing…? (otherwise you wouldn’t have root in your path and not even python2 would work).

So your situation is currently:

importing in Python 3.5.3 doesn’t work:

>>> import ROOT
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/localscratch/hyulee/root-v6-14-00/lib/ROOT.py", line 24, in <module>
import cppyy
File "/localscratch/hyulee/root-v6-14-00/lib/cppyy.py", line 61, in <module>
import libPyROOT as _backend
ImportError: dynamic module does not define module export function (PyInit_libPyROOT)

…but in Python 2.7.13 it does! So this might have to do something with missing support for Python 3?

correct? I think we need the help of @etejedor at this point :sweat_smile:

Dear @hkl

Currently, ROOT can only be built for a particular Python version. You built your ROOT for Python2 and you are trying to use it from Python3, that is why you see that error.

If you want to use ROOT with Python3, you need to rebuild it and make sure you specify the python3 executable. More information here:
https://root.cern.ch/building-root

Enric

Hi,

yes, this is the current status. To be more precise, I’m rebuilding ROOT now using

sudo cmake --build . -Dall=ON -Dpython=OFF -Dpython3=ON -Dpython_executable=/usr/local/bin/python3.7m/ -Dpython_library=/usr/local/lib/python3.7/ -Dpython_include_dir=/usr/local/include/python3.7m/

@etejedor
Thanks for your response, I’m trying to get the versions right this time. Is the above command correct? And do I have to add these options when executing cmake for the first time or also when doing cmake --build . afterwards?

Ok, just finished the build. According to the options, Python 3 should be supported, Python 2 not. But now import ROOT does not work in any Python version:

Python 3.7.1 (default, Oct 30 2018, 16:09:27)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
Traceback (most recent call last):
File "<stdin>", line 1, in <module>"
ModuleNotFoundError: No module named 'ROOT'

…same for Python 2.7.13 and 3.5.3.

PS.: This is in my .bashrc:

PATH=$PATH:/localscratch/user/root_6.14.04/
PATH=$PATH:/localscratch/user/root_6.14.04/bin/
PATH=$PATH:/localscratch/user/root_6.14.04/lib/
PATH=$PATH:/localscratch/user/root_6.14.04/bindings/pyroot/
PYTHONPATH=$PYTHONPATH:/localscratch/user/root_6.14.04/
PYTHONPATH=$PYTHONPATH:/localscratch/user/root_6.14.04/bin/
PYTHONPATH=$PYTHONPATH:/localscratch/user/root_6.14.04/lib/
PYTHONPATH=$PYTHONPATH:/localscratch/user/root_6.14.04/bindings/pyroot/

FYI (and depending on what you wanted to do initially with rootpy) you may want to try out uproot: it’s installable with pip.

Hi @hkl,

I would not set python to OFF, just specify the executable with -DPYTHON_EXECUTABLE=path/to/bin. Note that you need to include the binary itself in this path, right now you do not have it. If everything has been set up correctly, in your build directory you should see a CMakeCache.txt with a similar content to this:

//Path to a program.
PYTHON_EXECUTABLE:FILEPATH=/cvmfs/sft.cern.ch/lcg/views/LCG_rootext20180517python3/x86_64-centos7-gcc7-opt/bin/python

//Path to a file.
PYTHON_INCLUDE_DIR:PATH=/cvmfs/sft.cern.ch/lcg/views/LCG_rootext20180517python3/x86_64-centos7-gcc7-opt/include/python3.6m

//Path to a library.
PYTHON_LIBRARY:FILEPATH=/cvmfs/sft.cern.ch/lcg/views/LCG_rootext20180517python3/x86_64-centos7-gcc7-opt/lib/libpython3.6m.so

On the other hand, for ROOT to find its python module, please make sure you source thisroot.sh, this will properly set up your PYTHONPATH.

Cheers,
Enric

Hi Enric,

okay, I will recompile just using

sudo cmake -Dall=ON -Dpython3=ON -Dpython_executable=/usr/local/bin/python3.7m/ -Dpython_library=/usr/local/lib/python3.7/ -Dpython_include_dir=/usr/local/include/python3.7m/

What do you mean by including the binary itself in the path? In /usr/local/bin there are no executables, only a collection of Python scripts, shell scripts and “shared libraries” (such as /usr/local/bin/python3.7m)…maybe this is the wrong path? Thank you so much for your help.

PS.: Is it correct, that the build options (-Dpython3=ON etc.) are only stated with the “first” cmake command, but not with the second, proper build command cmake --build .?

This isn’t quite right, you’ve added libraries to the binaries path and added binaries to the python library path. I’d suggest using an environment module for each version of ROOT that you have installed:

The environment module should look something along the lines of this (which should be similar to what would go in your bashrc):

#%Module1.0

conflict root
set version 6.14.04
set prefix /opt/root/${version}
setenv ROOTSYS ${prefix}
append-path LIBRARY_PATH ${prefix}/lib
append-path PYTHONPATH ${prefix}/lib
append-path CMAKE_PREFIX_PATH ${prefix}
append-path JUPYTER_PATH ${prefix}/etc/notebook
append-path PATH ${prefix}/bin

Thanks, I will read up on environment modules and try to implement this. Could you please point out the wrong/redundant entries? I’m not sure which one’s which, except that /bin contains binaries and /lib contains libraries :sweat_smile: