pyROOT with Python2 works, pyROOT with Python3 does not

Hello everybody!
I am learning Pyhton and I’d like to use pyRoot but I have a problem: pyRoot works with Python2 but not with Python3.

When I use Python2 I get:

alessandro@alessandro-X550CC:~/Scrivania$ python
Python 2.7.17 (default, Nov  7 2019, 10:07:09) 
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT

Everything is fine! However, when I use Python3 I get:

alessandro@alessandro-X550CC:~/Scrivania$ python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.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/alessandro/Scrivania/root/lib/", line 24, in <module>
    import cppyy
  File "/home/alessandro/Scrivania/root/lib/", line 61, in <module>
    import libPyROOT as _backend
ImportError: dynamic module does not define module export function (PyInit_libPyROOT)

What’s the problem, please?
Thanks in advance to everybody!

_ROOT Version: 6.16
_Platform: Ubuntu


The issue is that you are using a ROOT installation that was built for Python2. Therefore, it works when you use the Python2 interpreter, but throws an error when you use Python3. Current PyROOT can only be built for one Python version, we have removed that limitation in a new PyROOT we are working on but it is still present in the current one.

If you want to use Python3, you will need to build ROOT with Python3, by specifying the right parameters of CMake:

in particular PYTHON_EXECUTABLE .

1 Like


Thank you for your answer and your help!

I have built ROOT with the CMake’s option you suggested:

cmake /home/alessandro/Scrivania/root/root-6.18.04 -DPYTHON_EXECUTABLE=/usr/bin/python3

But I get the following problem:

-- Looking for python
-- Found PythonInterp: /usr/bin/python3 (found version "3.6.9") 
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/ (found version "2.7.17") 
CMake Error at cmake/modules/SearchInstalledSoftware.cmake:444 (message):
  Version mismatch between Python interpreter (3.6.9) and libraries (2.7.17).

  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:159 (include)

I have also tried with the following line of CMake:

cmake /home/alessandro/Scrivania/root/root-6.18.04 -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/use/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/

But nothing changed.
What can I do, please?

Indeed the options you specified:

-DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/use/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/

should be enough to make it point to the right Python installation. Can you try the three options again from scratch, in a clean build?

@oshadura @amadio

The error you see usually means that you don’t have development libraries/headers for the version of Python that you are trying to build against. Please make sure that headers for Python3 are available in PYTHON_INCLUDE_DIR. I also recommend to only set PYTHON_EXECUTABLE, as CMake can detect the rest by itself. Cheers,

1 Like


Nothing has changed

I think of having all the headers, otherwise Python3 would not work, right? Python3 works perfectly when I do not use it with ROOT

The Python interpreter doesn’t need the headers. You only need them when compiling things against Python. Please make sure they are installed.

1 Like

Thank you very much! It works!

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