Hi forum, I think this question has a fairly basic answer, as far as I can tell it hasn’t already been asked.
I would like to use PyRoot in python3. When I set up ROOT I didn’t know what I was doing so I used all the default options. Going by this question to use ROOT in python3 the flag -DPYTHON_EXECUTABLE=/path/to/desired/python should be passed to make.
I currently have a fully operational ROOT setup. It can be launched with $root and imported to python 2 with import ROOT. I got there by following the instructions provided in README/INSTALL. What needs to be undone or removed so that I can do the make step again with the flags for python3?
That depends on what version of ROOT you are using, and if you installed it or you are only using it from the build directory. What I recommend is that you pass -DPYTHON_EXECUTABLE=$(which python3) to CMake, not make, then remove the bindings folder, recompile, and reinstall ROOT. It will no longer work with Python 2.x after that, as only one of Python 2.x or 3.x may be used. I hope this answers your question.
If you are using ROOT from git, you can use -Dpython_version=3 instead of the option shown above.
No, that’s the configuration step. To compile, you run cmake --build . or make in the build directory. Then you can install with cmake --build . --target install or make install. That way you don’t need to source the thisroot.sh script anymore, but you should use -Drpath=ON when configuring with CMake and then add the path to the directory where root.exe is installed (i.e. $ROOTSYS/bin) to your $PATH.
Please download and use ROOT 6.12/06, as it has many improvements over 6.10/08. Cheers,
Note that there are more dependencies on python in ROOT, e.g. TMVA (LLVM too, but only as part of the build process IIRC). You may want to do a full rebuild in a proper virtualenv for p3.
If your only concern is PyROOT, the dependency is fully captured in libPyROOT.so, which needs to be rebuild from scratch, but you can save the libPyROOT.so file and switch between p2 and p3 by pointing to each respective libPyROOT.so (through virtualenv or simply PYTHONPATH).
Aside, I don’t know whether cmake is already at the level of where the autoconf tools once were, but it used to be possible to simply run “make clean-pyroot” to clean PyROOT (rather than zapping the directory). Then run configure anew pointing to the new python, rebuild and done.
OK, well first I tried to change python2 to python3 by the steps above that included deleting the bindings directory. It reinstalled OK, but was still working in python2 and not python3. So I would say that dosn’t work.
Next I deleted the entire root and build directory and installed from scratch from the github. I followed the instructions above. Root seems to be working fine, but I cannot import it to python3 or python2. Both give the same import error;
670|09:06:32|~$ 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>
ImportError: No module named 'ROOT'
>>> exit()
670|09:06:44|~$ python2
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named ROOT
I noticed in this guide it says that $PYTHONPATH should contain $ROOTSYS/lib. Well I currently have neither a $PYTHONPATH nor a $ROOTSYS, so I added a line to my .bashrc_additions (which is sourced by .bashrc);
export PYTHONPATH="/home/myname/Root/build/lib"
This is probably a bit hacky, but it does appear to fix my problem. If there are any issues using pyroot in p3 I will post again.
Sorry, I forgot to mention that. If you install ROOT in some unusual place, Python has no way to know where your module is located, so this (even though it seems a bit hacky) is the way to tell Python where to look for PyROOT. It’s fine to put this in your bashrc, but please remember to update it when you update ROOT. Cheers,