RootPy installation - ROOT cannot be imported?

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:

Of course you could simply source thisroot.sh, but if you want to add them manually here is a set of environment variable that may be set. You may not need them all (JUPYTER_PATH, …), and there may be other you want to set as well (LD_LIBRARY_PATH, …), and others that shouldn’t need to be set, but may need to be set based on your configuration (MAN_PATH, …).

export ROOTSYS=/path/to/root
export LIBRARY_PATH=${LIBRARY_PATH}:${ROOTSYS}/lib
export PYTHONPATH=${PYTHONPATH}:${ROOTSYS}/lib
export CMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}:${ROOTSYS}
export JUPYTER_PATH=${JUPYTER_PATH}:${ROOTSYS}/etc/notebook
export PATH=${PATH}:${ROOTSYS}/bin

Hi @hkl

-Dpython_executable=/usr/local/bin/python3.7m/

is not a path to a binary, it is a path to a directory. See my example:

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

You need to specify there the full path to your python binary (perhaps /usr/local/bin/python3.7 ? )

Try to run which python3 to check where it is.

Thanks, I will add this (and source thisroot.sh)! Should I keep any of those:

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

Don’t do both. If you are happy with sourcing thisroot.sh, just do that and leave the environment variables alone.

Ok. If I would like to use ROOT in Jupyter, should I just source thisroot.sh and only add export JUPYTER_PATH=${JUPYTER_PATH}:/path-to-root/etc/notebook/?

It appears that JUPYTER_PATH is already set in thisroot.sh:

@ksmith: Thanks for this bit!

I’ve rebuilt ROOT using

sudo cmake -Dall=ON -Dpython3=ON -Dpython_executable=/usr/bin/python3.5m/ -Dpython_library=/usr/lib/python3.5/ -Dpython_include_dir=/usr/include/python3.5m/ /localscratch//root-6.14.04/
sudo cmake --build .

Once again, import ROOT works only in Python 2.7.13, even though I set -Dpython3=ON. In Python 3.5.3 I get:

>>> import ROOT
Traceback (most recent call last):
File “”, line 1, in
File “/localscratch//root_6.14.04/lib/ROOT.py”, line 24, in
import cppyy
File “/localscratch//root_6.14.04/lib/cppyy.py”, line 61, in
import libPyROOT as _backend
ImportError: dynamic module does not define module export function (PyInit_libPyROOT)

Also, for some reason, source thisroot.sh throws errors (bash: cd: too many arguments, bash: sed: No such file or directory, bash: grep: command not found) and I have to reboot in order for this to execute.

@etejedor: Doing which python3.5 gives /usr/bin/python3.5… Could it be that ROOT requires the use of exactly /python3 (instead of /python3.5, /python3.5m or /python3.7)? And maybe the trailing backslashes are causing trouble (files getting interpreted as folders)…

I’m rebuilding (again :sweat_smile:) using:

sudo cmake -Dall=ON -Dpython3=ON -Dpython_executable=/usr/bin/python3.5 -Dpython_library=/usr/lib/python3.5 -Dpython_include_dir=/usr/include/python3.5 /localscratch//root-6.14.04/

/edit: Okay, same result! import ROOT works in Python 2, but not Python 3. Not sure what I’m missing here… CMakeCache.txt did not contain any such long entries for either of the three options. For PYTHON_LIBRARY, do I need to explicitly point to a .SO-file?

Hi @hkl

Can you paste here what your CMakeCache.txt file contains for PYTHON_EXECUTABLE, PYTHON_INCLUDE_DIR and PYTHON_LIBRARY?

Enric

Hi,

these are the lines in CMakeCache.txt:

PYTHON_EXECUTABLE:FILEPATH=/usr/bin/python
PYTHON_INCLUDE_DIR:PATH=/usr/include/python2.7
PYTHON_LIBRARY:FILEPATH=/usr/lib/x86_64-linux-gnu/libpython2.7.so

It seems CMake didn’t use the options given by

sudo cmake -Dall=ON -Dpython3=ON -Dpython_executable=/usr/bin/python3.5 -Dpython_library=/usr/lib/python3.5 -Dpython_include_dir=/usr/include/python3.5 /localscratch/user/root-6.14.04/

for which the terminal output says

-- Found PythonInterp: /usr/bin/python (found version "2.7.13")
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so (found version "2.7.13")
-- Found NUMPY: /usr/local/lib/python2.7/dist-packages/numpy/core/include (found version "1.14.5")

It’s the caps, using upper-case commands sets the options right. After rebuild, import ROOT finally works in Python 3.5.3 and (as intended) does not in Python 2.7.13.

Unfortunately, both sudo pip install rootpy and sudo pip3 install rootpy still throw the error

ROOT cannot be imported. Is ROOT installed with PyROOT enabled?

Not sure whether this is relevant, but doing which python gives /usr/bin/python. The latter appears to be a link to /etc/alternatives/python, which again links to /usr/bin/python3.5m (the shared library). Everything good here, right?

As suggested here, I tried copying ROOT.py and libPyROOT.so into /usr/lib/, /usr/lib/python3.5, and /usr/lib/x86_64-linux-gnu, but this does not help either. Any ideas?

Hi @hkl

Since this is a rootpy issue, not ROOT, I am afraid I can’t be of much help. You were able to successfully import ROOT from the Python prompt, so I can’t really tell what rootpy is missing.

Enric

Okay, I will try uproot next as @sbinet suggested and leave this issue open. At the end, I want to work with data contained in ROOT files using Python (e.g. Jupyter).

Thanks a ton!

Hi @hkl

Please note you can also use PyROOT :slight_smile: without rootpy. We are here to assist you with any question you might have. What is exactly what you want to do?

Cheers,
Enric