# PyMVA crashes with conda ROOT

**URL:** <https://root-forum.cern.ch/t/pymva-crashes-with-conda-root/46945>\
**Category:** Newbie\
**Tags:** python, tmva\
**Created:** [September 22, 2021, 6:51pm UTC](https://root-forum.cern.ch/t/pymva-crashes-with-conda-root/46945 "2021-09-22T18:51:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsharankova](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@rsharankova](https://root-forum.cern.ch/u/rsharankova)\
**Post date:** [September 22, 2021, 6:51pm UTC](https://root-forum.cern.ch/t/pymva-crashes-with-conda-root/46945/1 "2021-09-22T18:51:01Z")

</div>

Hi,

I am using ROOT v6\_24\_6 installed with miniforge3 on macOS Big Sur.  
I was attempting to use PyKeras method in TMVA, but PyMVA initialization fails. I paste an iPython snippet that reproduces the crash, with error trace included.

```auto
(rootenv) $ ipython
Python 3.8.10 | packaged by conda-forge | (default, May 11 2021, 06:27:18) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.27.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from ROOT import TMVA

In [2]: TMVA.Tools.Instance()
Out[2]: <cppyy.gbl.TMVA.Tools object at 0x14d608dd0>

In [3]: TMVA.PyMethodBase.PyInitialize()
Error processing line 1 of /Users/rshara01-local/miniforge3/envs/rootenv/lib/python3.8/site-packages/google_auth-1.35.0-py3.9-nspkg.pth:

Fatal Python error: init_import_size: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "/Users/rshara01-local/miniforge3/envs/rootenv/lib/python3.8/site.py", line 169, in addpackage
    exec(line)
  File "<string>", line 1, in <module>
  File "/Users/rshara01-local/miniforge3/envs/rootenv/lib/python3.8/importlib/util.py", line 14, in <module>
    from contextlib import contextmanager
  File "/Users/rshara01-local/miniforge3/envs/rootenv/lib/python3.8/contextlib.py", line 5, in <module>
    from collections import deque
  File "/Users/rshara01-local/miniforge3/envs/rootenv/lib/python3.8/collections/ __init__.py", line 24, in <module>
    import heapq as _heapq
  File "/Users/rshara01-local/miniforge3/envs/rootenv/lib/python3.8/heapq.py", line 581, in <module>
    from _heapq import *
SystemError: initialization of _heapq did not return an extension module

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
...
SystemError: initialization of _heapq did not return an extension module

```

I saw what appears to be a similar issue (for PyROOT) being discussed here:  
[https://sft.its.cern.ch/jira/si/jira.issueviews:issue-html/ROOT-9950/ROOT-9950.html](https://sft.its.cern.ch/jira/si/jira.issueviews:issue-html/ROOT-9950/ROOT-9950.html)

I wonder if it is indeed the same thing, and if there is a workaround?

Thank you in advance,  
Ralitsa

* * *

_ROOT Version:_ 6.24.6 (from conda-forge)  
_Platform:_ MacOS 11.5.2  
\_Python Version: 3.8.10 (from conda)  
_Compiler:_ N/A

* * *

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [September 23, 2021, 8:46am UTC](https://root-forum.cern.ch/t/pymva-crashes-with-conda-root/46945/2 "2021-09-23T08:46:12Z")

</div>

@etejedor Can you comment?

---

<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:** [September 23, 2021, 9:51am UTC](https://root-forum.cern.ch/t/pymva-crashes-with-conda-root/46945/3 "2021-09-23T09:51:49Z")

</div>

Hi,  
The referenced issue was solved, we now build the PyROOT libraries with undefined symbols and we don’t link to the python libs anymore.

Perhaps some problem in `PyMethodBase.PyInitialize()` @moneta ?

I can’t reproduce with conda ROOT 6.24.02 though.

---

<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:** [September 23, 2021, 10:42am UTC](https://root-forum.cern.ch/t/pymva-crashes-with-conda-root/46945/4 "2021-09-23T10:42:25Z")

</div>

Also tried with 6.24.06 and I can’t reproduce either (this is Ubuntu). Perhaps @moneta can try on a mac? I installed with `conda create -c conda-forge --name root_624_6 root`

---

<div class="post-metadata">

**Author:** ![rsharankova](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@rsharankova](https://root-forum.cern.ch/u/rsharankova)\
**Post date:** [September 23, 2021, 9:43pm UTC](https://root-forum.cern.ch/t/pymva-crashes-with-conda-root/46945/5 "2021-09-23T21:43:47Z")

</div>

I get similar issue on a different macOS version, so it persists for me (though the error message is different this time).

On macOS 10.15.7, I created a fresh venv:  
`conda create -c conda-forge --name py38root624 python=3.8 root`

Then tried again:

```auto
In [1]: from ROOT import TMVA
In [2]: TMVA.Tools.Instance()
Out[2]: <cppyy.gbl.TMVA.Tools object at 0x7fcea8b6e2f0>
In [3]: TMVA.PyMethodBase.PyInitialize()
<FATAL> : Can't import __main__
***> abort program execution
---------------------------------------------------------------------------
runtime_error Traceback (most recent call last)
<ipython-input-3-abfda3a85476> in <module>
----> 1 TMVA.PyMethodBase.PyInitialize()

runtime_error: static void TMVA::PyMethodBase::PyInitialize() =>
    runtime_error: FATAL error

```

\_ROOT Version: 6.24.6 (from conda-forge)  
\_Platform: MacOS 10.15.7 (Catalina)  
Python version: 3.8.12 (from conda-forge)

---

<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:** [September 24, 2021, 8:06am UTC](https://root-forum.cern.ch/t/pymva-crashes-with-conda-root/46945/6 "2021-09-24T08:06:45Z")

</div>

@moneta was also able to reproduce with conda on MacOS but he has a different error (`pointer being freed was not allocated`).

I tried on a MacOS 10.15 with an installation of ROOT from source and I can’t reproduce, so it might be related to the Conda installation on MacOS. @chrisburr do you have an idea of what can be the issue here?

---

<div class="post-metadata">

**Author:** ![rsharankova](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@rsharankova](https://root-forum.cern.ch/u/rsharankova)\
**Post date:** [September 28, 2021, 1:55pm UTC](https://root-forum.cern.ch/t/pymva-crashes-with-conda-root/46945/7 "2021-09-28T13:55:19Z")

</div>

The resolved PyROOT issue I originally referenced suggests that similar issues can be expected for PyMVA as well in the presence of both static& dynamic libs. To quote  
` Note that I will discuss PyROOT, but PyMVA will need the same treatment.`  
I wonder if that was already addressed? Apologies if it has been, and this is something unrelated.

---

<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:** [September 28, 2021, 3:35pm UTC](https://root-forum.cern.ch/t/pymva-crashes-with-conda-root/46945/8 "2021-09-28T15:35:44Z")

</div>

I see your point now, thanks. I’m still not sure that is the cause of the issue you see, I leave it up to @moneta to decide what to do.

For reference, the PR that changed the way we build the PyROOT libraries to not link against libpython is:

> <https://github.com/root-project/root/pull/5643>
>
> …efined symbols
> 
> As a general rule, libraries containing C/C++ extensions that… are meant
> to be loaded from Python, should not be linked against libPython; the
> reason for this is that when these libraries are imported, Python has
> already loaded the libPython symbols, and if these are different from
> the ones used to link against the extension module a crash can happen.
> 
> libcppyy and libROOTPythonizations, being only imported from Python, do
> not need to link against libPython.
> 
> On the other hand, this is needed for libROOTTPython, which is used only
> form C++.

A similar thing could be tried with PyMVA.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [October 4, 2021, 9:40am UTC](https://root-forum.cern.ch/t/pymva-crashes-with-conda-root/46945/9 "2021-10-04T09:40:48Z")

</div>

Hi,  
We have a PR merged in master that should fix this Condo issue,

> <https://github.com/root-project/root/pull/9044>
>
> This PR fixes the way the returned reference to PyObject are used in PyMethodBas…e.
> 
> When returned objects are borrowed and they need to be reused we increase the reference count,
> but we make sure we don't decrease the count if it was not increased.
> This should fix an issue observed in the Conda builds.
> See https://root-forum.cern.ch/t/pymva-crashes-with-conda-root/46945
> 
> When the return objects are created ,we decrease the count if they are not used anymore.

Lorenzo
