I am trying to make a Docker image provided so I can hand it to my students to learn PyROOT with python 3x. I have an image working based on the one provided by CERN here: https://hub.docker.com/r/rootproject/root-ubuntu16/
I had to add this in a local Dockerfile:
FROM rootproject/root-ubuntu16
ENV PYTHONPATH ${PYTHONPATH}:/usr/local/lib/root/
Unfortunately this only works with python 2x. When trying to import ROOT in python 3x I get:
builder@0f9a7050cbe8:/$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] 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 "/usr/local/lib/root/ROOT.py", line 24, in <module>
import cppyy
File "/usr/local/lib/root/cppyy.py", line 61, in <module>
import libPyROOT as _backend
ImportError: dynamic module does not define module export function (PyInit_libPyROOT)
>>>
This appears to be because the libPyROOT is compiled for python 2x and won’t work with python 3x. Do I have to start from scratch in order to get it to work with python 3x?
Like you said, that Docker image has been built with Python2. We are working to remove this limitation, so that you can have a ROOT build that can work with both Python2 and Python3, but this is not yet there.
The pre-built images on Ubuntu16 have PyROOT compiled with Python 2.x only, as mentioned by Enric. If the underlying operating system is not important, you can try one of the recipes from here: https://github.com/root-project/root-docker to build an image with Python3 as default. In the mean time, I will prepare an image with Python 3.x as default and upload to our docker hub. I will let you know here once it’s there.
I just wanted to add that I get the same with pre-compiled Mac OX version.
~>python3.6
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[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 "<stdin>", line 1, in <module>
File "/Users/roberto/root/lib/ROOT.py", line 24, in <module>
import cppyy
File "/Users/roberto/root/lib/cppyy.py", line 61, in <module>
import libPyROOT as _backend
ImportError: dynamic module does not define module export function (PyInit_libPyROOT)
It would be really great to start having import ROOT to work in modern Python3 projects without need to compile ROOT from scratch.
For OSX at least, you can get ROOT from MacPorts. The only inconvenience is having to install MacPorts, but that’s easier than compiling ROOT itself (though not as easy as Docker). The ports are root5 and root6, and you have to select the +python variant that you want, then use the “port select python” mechanism to select the python you want to use.
On my Macbook I have root5 with python 2.7 installed alongside root6 with python 3.5. It’s pretty convenient for making scripts that work in both.
Our binaries distributed in the website are compiled to work with the default Python installation on Mac OS X, and that’s Python 2.7. However, if you install ROOT via Homebrew (see https://formulae.brew.sh/formula/root), it’s possible (and even the default) to install ROOT with Python 3.x support. However, you have to use both ROOT and Python from Homebrew for them to work together (which may require setting $PYTHONPATH). Cheers,