I have a problem with pyroot. When I try to import a ROOT histogram I get the same AttributeError all the time.
>>> from ROOT import TH1F
AttributeError: type object 'TArray' has no attribute '__getitem__'
During handling of the above exception, another exception occurred:
SystemError: <built-in method mro of ROOT.PyRootType object at 0x328fb18> returned a result with an error set
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'TH1F'
I also tried rootpy which doesn't work. Probably related?
I have Python 3.5 installed and did a clean install of ROOT with gcc 5.2.0. The Python module is listed when I run root-config --features
.
Any ideas? Or solutions?
The problem you're facing is related to a recent change in Python which solved wrong exception handling. A call in the Pythonize.cxx
wrapper tries to rename the __getitem__
attribute to the class TArray
which doesn't exist. This leads to an AttributeError which was ignored in Python until the new python3.5 release.
To restore the old behaviour, you need to modify the file Utility.cxx
in your $ROOTSYS/bindings/pyroot/src/
directory. Search for the method
Bool_t PyROOT::Utility::AddToClass( PyObject* pyclass, const char* label, const char* func )
which should be at around line 230. In this method is a if condition:
if ( ! pyfunc )
return kFALSE;
Here you need to replace the code above with the following lines:
if ( ! pyfunc ) {
PyErr_Clear();
return kFALSE;
}
The call of PyErr_Clear()
will fix this. Save the file and recompile your ROOT installation. This should fix the problem.
Edit: There's already a bug report for this issue: https://sft.its.cern.ch/jira/browse/ROOT-7640