Python class loaded with TPython unkown to C++ interpreter

Dear ROOT experts,

I am trying to import a python class to be used in a C++ ROOT macro. Following the instructions in ROOT: TPython Class Reference I am able to load the class into the interpreter and instantiate an object (and subsequently use it), however if I try to do the same in a macro, the object cannot be created:

Processing TestPyBindings.C...
In file included from input_line_8:1:
/Users/mhuwiler/cernbox/DoctoralThesis/Analysis/scripts/Ana/TestPyBindings.C:12:2: error: unknown type name 'MyPyClass'
        MyPyClass pyClass;

I did include TPython.h. If I do not create an object, the import is carried out successfully (display messages inside the python constructor show), but when creating an object the interpreter complains about unknown type. Forward declaration does not help.

Here is a minimal example:

print('creating class MyPyClass ... ')
import numpy as np
class MyPyClass:
   def __init__( self ):
      print('in MyPyClass.__init__')
   def gime( self, what ):
      return what

   def generateArray(): 
   	  a = np.ones((100))
   	  return a

And the C++ macro:

#include "ROOT/RDataFrame.hxx"
#include "TPython.h"

//class MyPyClass; 

void TestPyBindings() 
	// Load the python class
	MyPyClass pyClass; 


I am looking forward to your help!

Best Regards,


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.26/06
Platform: MacOS 10.15.7
Compiler: Apple clang version 12.0.0 (clang-1200.0.32.29)

Use, e.g.:

root -e 'TPython::LoadMacro("");' TestPyBindings.C

Or create a simple “rootlogon.C” file (in your current working directory):

{ // rootlogon.C
  TPython::LoadMacro(""); // Load the python class

The bottom line is that you must somehow get the class known to ROOT before it appears in macros.

Thank you very much!

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