PyROOT import error (PyInit_libPyROOT)

Dear PyROOT support,

I am trying to use PyROOT (Root 5.34) with Python 3.2.5.
The installation seems successfull (ROOT installation ok), but when I try to import ROOT, this is what I get:

(Py3.2_ENV)[root@ice Py3.2_ENV]# python
Python 3.2.5 (default, Jun 17 2013, 20:19:10)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

import ROOT
Traceback (most recent call last):
File “”, line 1, in
File “/LOCAL_GTB/Python3.2/Py3.2_ENV/root5.34/lib/root/ROOT.py”, line 100, in
import libPyROOT as _root
ImportError: dynamic module does not define init function (PyInit_libPyROOT)

My configuration:
./configure --prefix=/LOCAL_GTB/Python3.2/Py3.2_ENV/root5.34/ --enable-fftw3 --enable-fitsio --enable-gsl-shared --enable-mysql --enable-python --enable-soversion --with-fftw3-incdir=/LOCAL_GTB/fftw3.3.3/include --with-fftw3-libdir=/LOCAL_GTB/fftw3.3.3/lib --with-python-incdir=/LOCAL_GTB/Python3.2/include/python3.2 --with-python-libdir=/LOCAL_GTB/Python3.2/lib/ --disable-ssl

I am using virtualenv to install PyROOT for multiple Python versions: I did the same for Python 2.7 and everything works.

Could you please help me to figure out what I am missing?

Thanks a lot,
Valentina

Valentina,

the ‘PyInit_’ convention is specific to p3 (in p2, ‘init’ is used as a prefix), so it’s trying to load the p2 build library in this case. Did your virtualenv change take into account modifying PYTHONPATH to be /LOCAL_GTB/Python3.2/Py3.2_ENV/root5.34/lib:$PYTHONPATH?

You can look at sys.path to see which paths are considered.

Cheers,
Wim

Hi, sorry to reopen an old topic, but I’m having the same issue with Python 3.5 and I think ROOT might have changed enough in the last two years that the given solution is no longer applicable.

I installed ROOT in the simple way, by just running the .dmg file that I downloaded for OSX 10.10. Now “import ROOT” works fine for a Python 2.7 virtualenv, but Python 3.5 gives the following:

[quote]Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.

import ROOT
Traceback (most recent call last):
File “”, line 1, in
File “/Applications/root_v6.06.02/lib/ROOT.py”, line 24, in
import cppyy
File “/Applications/root_v6.06.02/lib/cppyy.py”, line 60, in
import libPyROOT as _backend
ImportError: dynamic module does not define module export function (PyInit_libPyROOT)[/quote]

My Python 3.5 is installed in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5, and there is no folder called rootX.XX in there. As far as I can tell, the only ROOT stuff on my system is in /Applications/root_v6.06.02. My current PYTHONPATH variable, as given by Python itself, is “/Library/Frameworks/Python.framework/Version/3.5/lib:/Applications/root_v6.06.02/lib”.

What should I do to make pyROOT work in 3.5?

Thanks,

Hi,

no, is same issue. To run PyROOT with p3, you have to build libPyROOT for p3. Aside, I’m out of it, but I don’t think p3 is supported (yet).

Cheers,
Wim

As Wim says, you need to build ROOT (aka PyROOT.so) with Pyhon3 to use it from Python3. If you have both Python installations in your system, you can force the build with Python3 with the option -Dpython3=ON.
Not everything is working with Python3 yet.

1 Like

Is there a good overview somewhere, where one can see what works under Python 3 already?

1 Like

all roottest tests run w/ p3 (was the last thing I did); never used p3 myself, though, so “real life” may have discrepancies

1 Like

Hi,

all tutorials run with Py3 as well and we have quite many users that take advantage of Py3 and ROOT with the SWAN service. I think it’s ready for prime time (for 6.08 and especially for 6.10).

Cheers,
D

1 Like

Just to clarify is it possible to have python=ON and python3=ON simultaneously or do we need to have separate builds? If this is true, why does the CMake file not through an error if both are set to on?

Hi,

it is not yet possible to have Py2 and Py3 simultaneously supported by the same build unfortunately. Thanks for the suggestion! We definitively need to make CMake error out if by chance this is tried by the user.

Cheers,
D

well, it’s really a limitation of the build and the way the bindings are created.
it’s otherwise perfectly technically possible to achieve this (I am doing it with gopy which generates python bindings to Go packages, allowing to use Go from Py2, Py3, PyPy2, PyPy3 and (possibly, but not tested so presumably broken) IronPython)

</pedantic>

What makes this even more confusing (at least to me) is that if python points to python3 and you build it with python=ON and python3=OFF, it will use that default python and build it for Python 3…

I agree with @graipher, the tags are confusing. In addition, I have not yet been able to properly build the python3 bindings. I’ve tried every combination, and I either end up with python2 bindings or nothing at all. I don’t really need the python 3 at the moment, but was trying to ensure that my python 2 was forward compatible.

At least there I was more successful, because I managed to build the Python 3 bindings this way (by accident, though, which is how I discovered this).

Hi All,
While getting the same errors as reported above, installing the latest version (root-6.12.04) on my mac, I’m reading this thread but am not sure what is the solution :frowning:
I have python3 (v3.5.1 installed with homebrew) on my mac and am forcing the root installation with it using -Dpython3=ON.
Here’s the entire installation procedure:

cmake ../root-6.12.04 -DCMAKE_INSTALL_PREFIX=<myprefix> -Dgnuinstall=ON -Dpython3=ON -Droofit=ON
cmake --build .                  ## [ or simply "make -j<N>" on Unix systems ]
cmake --build . --target install ## [ or simply "make install" on Unix systems ]

It seems to install well and I can open root at least.
However, when trying to do something simple with python like:

#!/usr/bin/env python
import os, sys
import math
import ROOT
from ROOT import *
ROOT.gROOT.SetBatch(1)
ROOT.gSystem.Load("libRooFit")

I get this error:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    import ROOT
  File "my_root_v6.12.04_python_v3/lib/root/ROOT.py", line 24, in <module>
    import cppyy
  File "my_root_v6.12.04_python_v3/lib/root/cppyy.py", line 61, in <module>
    import libPyROOT as _backend
ImportError: dynamic module does not define module export function (PyInit_libPyROOT)

What have I missed :grimacing:?
Cheers,
Noam

The python3 flag does not work the way you think it would. You probably have python 2 as the default python?

JIRA 9033

Hi @ksmith,
I confirm it works after manually setting the python executable, headers and library for cmake - please see below if it helps for someone in the future:

#!/bin/sh
base=/usr/local/opt/python3/Frameworks/Python.framework/Versions/3.5 ### only in my case 
target=/some/path/to/desired/local/prefix/of/installation
cmake ../root-6.12.04 -DCMAKE_INSTALL_PREFIX=${target} -DPYTHON_EXECUTABLE=${base}/bin/python3 -DPYTHON_INCLUDE_DIR=${base}/Headers -DPYTHON_LIBRARY=${base}/lib/libpython3.5m.dylib -Dgnuinstall=ON -Dpython3=ON -Droofit=ON -Dminuit2=ON
cmake --build .
cmake --build . --target install

I now get the ImportError: "from ROOT import *" is not supported in Python 3 error but I think this is a known issue (https://sft.its.cern.ch/jira/browse/ROOT-8931). Is there a workaround for this backward-incompatibility?

Cheers,
Noam

Glad to hear you’ve gotten it sorted.

What do you mean by backward incompatibility? I assume you are attempting to write for both python 2 and 3? In general I do not use the import * option as it clutters the namespace. Adding ROOT. before the classes does not seem overly arduous for me and avoids this issue.

Hi All,

I’m having a similar problem with the newest Root versions 6.18/00 and 6.16/00, but when I use the exact same install procedure in version 6.12.06 it works mostly as expected. Let me describe:

The cmake configuration I use during install from my build directory is just:

cmake ../root-versionNumber/ -Dpython=ON -Dpyroot_experimental=ON -DPYTHON_EXECUTABLE=/usr/bin/python3

Using the compiled 6.12.06, when I try “root --notebook” the Jupyter notebook loads correctly on the local server. But when I use the compiled 6.18 (same thing happens in 6.16 as well) it crashes with this reference to python 2.7:

Traceback (most recent call last):

  File "/usr/local/bin/jupyter-notebook", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/jupyter_core/application.py", line 267, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 657, in launch_instance
    app.initialize(argv)
  File "</usr/local/lib/python2.7/dist-packages/decorator.pyc:decorator-gen-7>", line 2, in initialize
  File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/notebook/notebookapp.py", line 1630, in initialize
    self.init_webapp()
  File "/usr/local/lib/python2.7/dist-packages/notebook/notebookapp.py", line 1378, in init_webapp
    self.jinja_environment_options,
  File "/usr/local/lib/python2.7/dist-packages/notebook/notebookapp.py", line 159, in __init__
    default_url, settings_overrides, jinja_env_options)
  File "/usr/local/lib/python2.7/dist-packages/notebook/notebookapp.py", line 252, in init_settings
    allow_remote_access=jupyter_app.allow_remote_access,
  File "/usr/local/lib/python2.7/dist-packages/traitlets/traitlets.py", line 556, in __get__
    return self.get(obj, cls)
  File "/usr/local/lib/python2.7/dist-packages/traitlets/traitlets.py", line 535, in get
    value = self._validate(obj, dynamic_default())
  File "/usr/local/lib/python2.7/dist-packages/notebook/notebookapp.py", line 867, in _default_allow_remote
    for info in socket.getaddrinfo(self.ip, self.port, 0, socket.SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

Has something changed at the CMake configuration stage for Python 3 integration? Or did a bug creep in somewhere?

Thanks,

Mike

Hi Mike,
This is not related to the Python version, it is another error.

It has to do with a bug in Jupyter, here you can find more information:

https://sft.its.cern.ch/jira/browse/ROOT-9920

The bug of Jupyter is triggered when including the configuration option c.Notebookapp.ip = '*', which is used by ROOT. You can either move to a newer version of Jupyter or go to $HOME/.rootnb/jupyter_notebook_config.py and remove the aforementioned option.

1 Like