How much do I have to uninstall to go from python2 to python3

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?

Thanks for your time,
Day

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.

2 Likes

In order to launch it with root I must source bin/thisroot.sh every time, that indicates it’s not installed right?

I am a little confused by your instructions, you say pass -DPYTHON_EXECUTABLE=/path/to/desired/python to CMake, is that not compiling?

I got the version from https://root.cern.ch/download/root_v6.10.08.source.tar.gz

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,

1 Like

Oh ok, thanks I hadn’t realised that was a step needed.

So, for completeness if someone wanted to set up from git with python3 they would do;

$ git clone https://github.com/root-project/root.git
$ mkdir buildDir
$ cd buildDir
$ cmake -Dpython_version=3 ../root
$ cmake --build . --target install

If they already had a version of ROOT and wanted to change it from python2 to python3 this might work;

$ cd buildDir
$ cmake -DPYTHON_EXECUTABLE=/path/to/desired/python ../root

$ rm -r …/root/bindings

$ cmake --build . --target install

Does that look right?

Almost, you just have to remove buildDir/bindings, not from the source directory. Then it’s correct.

1 Like

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.

Thank you all for your advice and help.

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,

1 Like

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