Problem calling ROOT.TExec from a python script

Dear rooters,

I have a problem with using TExec from a python script, which I use to setup custom color palettes.
A similar issue (also related with TExec) occured to me with the same script some time ago: see Multiple color palettes with python freezes program.

The following dummy script reproduces the problem:

#!/usr/bin/env python
import ROOT
import ctypes

ROOT.gROOT.SetBatch(1)
ROOT.gStyle.SetOptStat(0)

C = ROOT.TCanvas('C', 'C', 500, 500)
xmax = 5.
h1 = ROOT.TH2F('h1', '', 200, -xmax, xmax, 200, -xmax, xmax)
for i in range(0, 100000, 1):
    a, b = ctypes.c_double(), ctypes.c_double()
    ROOT.gRandom.Rannor(a, b)
    h1.Fill(a.value, b.value)

h1.Draw('col')
dt = ROOT.TDatime()
t = ROOT.TText(-0.9 * xmax, 0.9 * xmax, 't')
t.SetTextAlign(13)
t.SetTextSize(.05)
t.SetTextColor(4)
t.Draw()
ex = ROOT.TExec('ex', 'TPython::Exec("dt.Set()");TPython::Exec("t.SetTitle(dt.AsString())")')
ex.Draw()
ROOT.gPad.Update()
C.Print('./texec_problem.pdf')

Error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/delaossa/local/miniconda3/envs/root_base/lib/python3.8/site-packages/ROOT/__init__.py", line 22, in <module>
    import cppyy
  File "/Users/delaossa/local/miniconda3/envs/root_base/lib/python3.8/site-packages/cppyy/__init__.py", line 60, in <module>
    importlib.import_module(libcppyy_mod_name)
  File "/Users/delaossa/local/miniconda3/envs/root_base/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
SystemError: initialization of libcppyy3_8 did not return an extension module
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'dt' is not defined
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 't' is not defined

My system is macOS BigSur 11.5.1 and I installed the root_base package with conda:

conda install -c conda-forge root_base=6.24.2=py38hc87488e_0

Python version:

Python 3.8.10 | packaged by conda-forge | (default, May 10 2021, 22:58:09)
[Clang 11.1.0 ] on darwin

The script works just fine on a Linux machine with the same root_base version and python 3.8.10.

Any ideas? Thank you very much!

Hello,

It could be a problem with the installation, according to this message:

SystemError: initialization of libcppyy3_8 did not return an extension module

What happens if you do import libcppyy3_8 directly from the Python prompt? Does it fail with the same error?

Hello,
import libcppyy3_8 goes fine (no warnings or anything):

Python 3.8.10 | packaged by conda-forge | (default, May 10 2021, 22:58:09)
[Clang 11.1.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import libcppyy3_8
>>>

However, import libcppyy says something else:

Python 3.8.10 | packaged by conda-forge | (default, May 10 2021, 22:58:09)
[Clang 11.1.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cppyy
/Users/delaossa/local/miniconda3/envs/root_base/lib/python3.8/site-packages/cppyy_backend/loader.py:113: UserWarning: No precompiled header available (/Users/delaossa/local/miniconda3/envs/root_base/lib/python3.8/site-packages/cppyy_backend/etc not writable); this may impact performance.
  warnings.warn('No precompiled header available (%s); this may impact performance.' % msg)
>>>

Does it help to pin down the problem?
Thank you very much for looking into this!

Hello,

Looks like the libcppyy library is fine, no idea why it complains in the first case. The warning when importing cppyy is unrelated.

I tried the code above both with a conda ROOT installation (6.24.2, Py3.9) and master from source and I can’t reproduce.

Perhaps recreating the environment would help?

What happens if you do:

conda create -c conda-forge root_base=6.24.2 --name root_624
conda activate root_624

and try to run the code again?

Hello,
I get exactly the same error.

Python 3.9.6 | packaged by conda-forge | (default, Jul 11 2021, 03:36:15)
[Clang 11.1.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
root
   ------------------------------------------------------------------
  | Welcome to ROOT 6.24/02                        https://root.cern |
  | (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Jul 03 2021, 08:06:00                      |
  | From tag , 28 June 2021                                          |
  | With                                                             |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

and the error:

python texec_problem.py
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/delaossa/local/miniconda3/envs/root_624/lib/python3.9/site-packages/ROOT/__init__.py", line 22, in <module>
    import cppyy
  File "/Users/delaossa/local/miniconda3/envs/root_624/lib/python3.9/site-packages/cppyy/__init__.py", line 60, in <module>
    importlib.import_module(libcppyy_mod_name)
  File "/Users/delaossa/local/miniconda3/envs/root_624/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
SystemError: initialization of libcppyy3_9 did not return an extension module
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'dt' is not defined
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 't' is not defined
(bool) false
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'dt' is not defined
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 't' is not defined
(bool) false
Info in <TCanvas::Print>: pdf file ./texec_problem.pdf has been created

Did you try with a Mac too?
Could be a matter of the environment over which my conda is installed?
I have Command_Line_Tools_for_Xcode_12.5.1 and the latest Homebrew installed.
Cheers

No, I tried on Linux, I’ll try to reproduce on a Mac.

@chrisburr have you seen this before?

Could be a matter of the environment over which my conda is installed?

You could perhaps try cleaning that environment?

I can reproduce on Mac 11, this is enough to see the libcppyy error:

>>> import ROOT
>>> ROOT.TPython.Exec("")
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/sftnight/miniconda3/envs/root_624/lib/python3.9/site-packages/ROOT/__init__.py", line 22, in <module>
    import cppyy
  File "/Users/sftnight/miniconda3/envs/root_624/lib/python3.9/site-packages/cppyy/__init__.py", line 60, in <module>
    importlib.import_module(libcppyy_mod_name)
  File "/Users/sftnight/miniconda3/envs/root_624/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
SystemError: initialization of libcppyy3_9 did not return an extension module

So it’s an issue with TPython, still don’t know the reason though.

I can’t reproduce on the same machine with a ROOT installation built from source. It might be something related to the conda installation on MacOS then, @chrisburr any thoughts? The problem seems to happen during the initialization of TPython.

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