Fixing python3 imports for UFO models

Asked by Zachary Marshall

Hi authors,

I'd really like to see if we can improve upon the current find_ufo_path:

https://github.com/mg5amcnlo/mg5amcnlo/blob/main/models/import_ufo.py#L69

to make the importing of models a bit more pythonic. That'd be a nice step towards giving us a little more power over the organization of our models. For a typical python module, you can do an:

import my_module
my_module.__path__

to get back the path to the model in case that's needed for anything. That means we could do something like

try:
  import my_model
  return my_model.__path__

Of course, please let me know if there's something fundamental I'm missing here that would make this a very bad idea. Assuming it's not, that works nicely in python2, e.g.:

>>> import vector_LQ_UFO
>>> vector_LQ_UFO.__path__
['/cvmfs/atlas.cern.ch/repo/sw/Generators/madgraph/models/latest/vector_LQ_UFO']

In python3, relative imports are no longer allowed (see e.g. https://nerdparadise.com/programming/python/import2vs3). That means when doing this import of the model, I hit a python error:

>>> import vector_LQ_UFO.propagators
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/cvmfs/atlas.cern.ch/repo/sw/Generators/madgraph/models/latest/vector_LQ_UFO/__init__.py", line 1, in <module>
    import object_library
ModuleNotFoundError: No module named 'object_library'

because the __init__.py of the UFO models use a whole bunch of relative imports:

import object_library

import particles
import couplings
import lorentz

instead of absolute imports, like:

from . import object_library
from . import particles
from . import couplings
from . import lorentz

This model is otherwise "python3-ready", except for the relative imports that show up everywhere (and this seems generically true for all the UFO models we use, I'm not just picking on whoever wrote this particular model).

The question is: is there any issue with changing these to absolute imports and moving in a direction where this is a bit more pythonic? As I said, I'm happy to help implement some of these changes, and to have them protected behind e.g. try/except blocks so that they won't affect users who don't want to use them, but I think it'd be a nice thing to do.

Thanks,
Zach

Question information

Language:
English Edit question
Status:
Expired
For:
MadGraph5_aMC@NLO Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Olivier Mattelaer (olivier-mattelaer) said :
#4

In a way this should be push request at the FeynRules tracking system (if they still have it).

But only new model will have the new linking mode in that case.
I also remember that such format was problematic with some version of python (likely python2 and/or some old version of python3)
and this is why I did not put such change in the "update model" command that MG5aMC is proposing.

Cheers,

Olivier

Revision history for this message
Launchpad Janitor (janitor) said :
#5

This question was expired because it remained in the 'Open' state without activity for the last 15 days.