# Build a python module wrapping ROOT classes

**URL:** <https://root-forum.cern.ch/t/build-a-python-module-wrapping-root-classes/37195>\
**Category:** ROOT\
**Tags:** python\
**Created:** [December 13, 2019, 2:11pm UTC](https://root-forum.cern.ch/t/build-a-python-module-wrapping-root-classes/37195 "2019-12-13T14:11:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cosimoNigro](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/cosimonigro/32/20452_2.png) [@cosimoNigro](https://root-forum.cern.ch/u/cosimoNigro)\
**Post date:** [December 13, 2019, 2:11pm UTC](https://root-forum.cern.ch/t/build-a-python-module-wrapping-root-classes/37195/1 "2019-12-13T14:11:43Z")

</div>

Dear (py)ROOTers,

I have a code built on ROOT, let us call it `MyPackage`, that I would like to wrap in a python module, let us call it `mypackage`.

This topic

> [@Wrapping ROOT classes for Python](https://root-forum.cern.ch/t/wrapping-root-classes-for-python/21845):
>
> Hello rooters, I would like to create a python wrapper for a C++ class derived from TNamed. According to this: [root.cern.ch/pyroot](https://root.cern.ch/pyroot) "PyROOT is a Python extension module that allows the user to interact with any ROOT class from the Python interpreter. This is done generically using the ROOT dictionary, therefore there is no need to generate any Python wrapper code to include new ROOT classes. […]" Since my class has a dictionary generated with rootcling and it can be used directly from the ROO…

was very useful in importing the ROOT classes of the aforementioned code in a simple python script (even in a python jupyter notebook actually!).

I’d like to go beyond that and build a python interface to the code, such that a user can install the python module and use the underlying ROOT classes from python.  
Fundamentally I do not want a user to do the `gSystem/gInterpreter` loading and then

```python
from ROOT import MyPackageClass

```

I’d like him to be able to directly do

```python
from mypackage import mypackageclass

```

What would be the proper way of doing it?  
From my limited experience I can only think to create a series of python modules, each wrapping the corresponding classes in `MyPackage/include/`and then gather all of them in a module with ` __init__.py`. Schematically:

```auto
.
├── include
│ ├── FirstClass.h
│ ├── SecondClass.h
│ └── ThirdClass.h
└── wrapper
    ├── __init__.py
    ├── firstclass.py
    ├── secondclass.py
    └── thirdclass.py

```

Additionally I do not know to which level I’d have to expose the class.  
Is there a cleaner and more and efficient way to realise it?  
Has somebody already done it and has suggestion?

Thanks for your guidance.

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [December 16, 2019, 8:45am UTC](https://root-forum.cern.ch/t/build-a-python-module-wrapping-root-classes/37195/2 "2019-12-16T08:45:15Z")

</div>

Hi,

I would not advise to create Python wrappers for your C++ classes because that is actually what PyROOT automatically does, which is what is cool about PyROOT 🙂

If you are concerned about the user having to load the headers / libraries and you want to hide that from them, why don’t you do it in the initialization of your module?

```python
import mymodule # makes the header / library known to the ROOT interpreter
from ROOT import FirstClass
...

```

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [December 30, 2019, 8:45am UTC](https://root-forum.cern.ch/t/build-a-python-module-wrapping-root-classes/37195/3 "2019-12-30T08:45:24Z")

</div>

Closed on December 30, 2019, 8:45am UTC

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