Read .root files with Python on Windows

My main concern is that a root file produced with v5.34 would not be fully readable from v6.* . Could this be possible?

I would be surprised. If it would be the case, please open a Jira ticket or at least report it on this forum.

I have installed ROOT v6.22.02 and set the right ENV variables (ROOTSYS, PATH and PYTHONPATH) up. I have pyhton 3.7.6 with anaconda 3 acivated.

I want to use PyROOT on Visual Studio Code. From a Jupiter Notebook I call:

import ROOT as root

but I get

**ModuleNotFoundError** Traceback (most recent call last) in **----> 1 ****import****** ROOT **as** root **ModuleNotFoundError** : No module named 'ROOT'

Should I install pip install or conda install the root package?

1 Like

I just downloaded root_v6.22.02.win32.vc16.zip, unzipped it in the Downloads folder, and then simply:

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.4.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'

C:\Users\bellenot>cd Downloads

C:\Users\bellenot\Downloads>root\bin\thisroot.bat

C:\Users\bellenot\Downloads>python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
Warning in <TClassTable::Add>: class ROOT::Detail::TTypedIter<class TEnumConstant> already in TClassTable
>>>

So as you can see, it works just fine. Can you try it the same way?

>>> import ROOT
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\root\6.22.02vs19_aclic\bin\ROOT.py", line 24, in <module>
    import cppyy
  File "C:\root\6.22.02vs19_aclic\bin\cppyy.py", line 61, in <module>
    import libPyROOT as _backend
ImportError: DLL load failed: %1 is not a valid Win32 application.
>>>

that’s what I get in the terminal! Should I import some other modules?
Thank you for the help!

Did you follow the exact same procedure that what I showed? Is it a 32 bit Python?

Yes I did.
It seems to me a 64 bit python, I am wrong?

Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32

Yes, please try with 32 bit Python. ROOT is 32 bit only

1 Like

There is something that I am doing wrong with the evirnomental variables. If I call root from the terminal I get

C:\Users\Giammarco>root
'root' is not recognized as an internal or external command,
operable program or batch file.

But if I double click on the root.exe in each /bin folder of each version they run perfectly.

As a consequence python 32 bit (with an acrtive environment) throws the following:

(anaconda32) C:\Users\Giammarco>python
Python 3.8.3 (default, Jul  2 2020, 17:28:51) [MSC v.1916 32 bit (Intel)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'ROOT'
>>>

My user environemental variables are
ROOTSYS = C:\root\6.22.02vs19_aclic;C:\root\5.34.36vs13_aclic;C:\root\5.34.38vs13_aclic;
PATH = %ROOTSYS%\bin

What do you think?

This cannot work. Either you change %ROOTSYS% to point to the only one version of ROOT you want to use, or you don’t set those variables at all and you call C:\root\6.22.02vs19_aclic\bin\thisroot.bat, or C:\root\5.34.36vs13_aclic\bin\thisroot.bat, or C:\root\5.34.38vs13_aclic\bin\thisroot.bat in a command prompt to properly set-up the environment variables before starting python

Ok, like this is working!

Python 3.8.3 (default, Jul  2 2020, 17:28:51) [MSC v.1916 32 bit (Intel)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
Warning in <TClassTable::Add>: class ROOT::Detail::TTypedIter<class TEnumConstant> already in TClassTable
>>>

Nevertheless it is not a sustainable solution because I will use the v6.22 for python and the other 5.34.x for my current analysis! What’s your way of hopping among multiple versions of root on the same machine?

As I said, I use the thisroot.bat file to use different versions at the same time (you can have as many different versions running in their own command prompt session). And I know many people doing the same on different platforms

ok, but this implies I need to change the ROOTSYS environmental variable too everytime! Am I getting it right?

No, the thisroot.bat script does it for you, it is the purpose of this script

Hi, there is again something I am doing wrong. Now if I type “root” from a simple cmd I get the output

 'root' is not recognized as an internal or external command,
operable program or batch file.

but if I do the same from a x86 native cross tool comand, prompt then everything works.

the same for improting it in python…

C:\Users\Giammarco>python
Python 3.8.3 (default, Jul  2 2020, 17:28:51) [MSC v.1916 32 bit (Intel)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import root
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'root'
>>>

This is a problem for my VScode IDE, because when I call
import root as ROOT
I get the console output

Traceback (most recent call last):
  File "c:\Users\Giammarco\Desktop\TESTVS19\tets.py", line 3, in <module>
    import root as ROOT
ModuleNotFoundError: No module named 'root'

Any ideas?

root on Windows is still a nightmare…

As I already said, you must call thisroot.bat before running root, and root must run in a x86 Native Tools Command Prompt for VS 2019, or call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars32.bat" from a regular command prompt, or properly set the necessary Visual C++ environment variables. And from VS Code you probably have to set the proper environment. And root on Windows is working well if you follow the instructions and if you understand how the environment variables work, which has nothing to do with ROOT…

I am calling thisroot.bat from the x86 prompt, but previously this would have set the right variables in order to call root from a general cmd. Are you sugesting I should call thisroot.bat everytime before running root? This was not the case with my previous 5.34 with VS17.

properly set the necessary Visual C++ environment variables? I followed the standard suggested procedure from VS19 instalaltion wizard. Is there anything more to do?

I meant the integration of root on VS is still a nightmare. ROOT per se is rocking.

ROOT v5.34 didn’t require Visual C++ to be installed at all. Now ROOT 6 has a JIT compiler and it need the C++ header files from the same version of Visual C++ than the one used to build ROOT. And you only need to call thisroot.bat for curent command prompt session, and it will be valid for this session only.

Those environment variables are set for the Command Prompt for VS 2019, not globally. if you need them elsewhere (e.g. in a standard Windows command prompt), then you need either to call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars32.bat" or to manually set them.

I’m not sure to understand what you’re talking about… Are you talking about the Visual Studio IDE or the VS Code Editor?

I am getting this as a prompt. I am using 32-bit Python, version 3.9.2 and version 6.22/06 version ROOT on windows. Please help.

C:\root_v6.22.06>cd bin
C:\root_v6.22.06\bin>thisroot.bat
C:\root_v6.22.06\bin>cd…
C:\root_v6.22.06>cd store
C:\root_v6.22.06\store>python
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:30:23) [MSC v.1928 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.

import math
import ROOT
Traceback (most recent call last):
File “”, line 1, in
File “C:\root_v6.22.06\bin\ROOT.py”, line 24, in
import cppyy
File “C:\root_v6.22.06\bin\cppyy.py”, line 61, in
import libPyROOT as _backend
ImportError: DLL load failed while importing libPyROOT: The specified module could not be found.

Please don’t reply to an old topic, but open a new one instead. And for your question, see Using PyRoot on Windows 10