Home | News | Documentation | Download

Import Error: Can't Import root with python

I installed Ubuntu on my Windows 10 computer. I have python installed on a certain directory. After opening the ubuntu shell, I type cd /mnt/c/1. Then I type in ls and I see that I have Python 3.7.4 installed. Now, if I type root, then I am able to use root. However, if I have a python file that has import ROOT, I get an import error. In other words if I type,

/mnt/c/1$ python /mnt/c/1/writeroot.py
Traceback (most recent call last):
  File "/mnt/c/1/writeroot.py", line 2, in <module>
    import ROOT
  File "/home/cucip/builddir/lib/ROOT.py", line 24, in <module>
    import cppyy
  File "/home/cucip/builddir/lib/cppyy.py", line 61, in <module>
    import libPyROOT as _backend
ImportError: dynamic module does not define module export function (PyInit_libPyROOT)

If I type /mnt/c/1$ python
>>>import ROOT

I get this error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/cucip/builddir/lib/ROOT.py", line 24, in <module>
    import cppyy
  File "/home/cucip/builddir/lib/cppyy.py", line 61, in <module>
    import libPyROOT as _backend
ImportError: dynamic module does not define module export function (PyInit_libPyROOT)

However, if I type:

/mnt/c/1$ root

I get

/mnt/c/1$ root
   ------------------------------------------------------------
  | Welcome to ROOT 6.18/02                  https://root.cern |
  |                               (c) 1995-2019, The ROOT Team |
  | Built for linuxx8664gcc on Sep 05 2019, 00:50:00           |
  | From tag , 23 August 2019                                  |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

root [0] 2+2
(int) 4

How can I fix these errors?


ROOT Version: 6.18/02
Platform: Not Provided
Compiler: Not Provided


Hi,
there seems to be a mismatch between the python version ROOT was built against and the python in /mnt/c/1.

How did you get this ROOT installation? What does root-config --python-version say?

The two most likely solutions are either recompiling ROOT making sure it picks up the python in /mnt/c/1 or using ROOT from a python with version equal to what root-config --python-version reports.

@etejedor might be able to comment further

Cheers,
Enrico

@eguiraud already gave you the right answer: the error you see is due to the fact that you are running your Python script with a Python version that is different from the one ROOT was built with. Intuitively, you are using Python3 but ROOT was built against a Python2 installation?

how do i build root against a python 3 version?
I uninstalled everything and then reinstalled python 3 and root yet when I write root-config --python-version it writes 2.7.15? What is the easiest way to fix this?
thanks

You need to build ROOT from sources, you can find instructions here:
https://root.cern.ch/building-root

In order to tell CMake which python you want to build ROOT against, you need to specify the PYTHON_EXECUTABLE variable, e.g.
cmake -DPYTHON_EXECUTABLE=/usr/bin/python3 …

Just to clarify why root-config --python-version always returns 2.7.15: that’s the python version ROOT was compiled against. In general, the binaries we distribute are compiled against the default python version for a given distribution. So the binaries for Ubuntu are compiled with python 2 support, because that’s the default python on that Ubuntu version.

Cheers,
Enrico