PyORBIT-Collaboration / PyORBIT3

Python3 compatible version of PyORBIT
MIT License
5 stars 14 forks source link

Make C++ submodules importable #16

Open azukov opened 6 months ago

azukov commented 6 months ago

Currently if a C++ module has a submodule the import is not working properly as the module is not a package. E.g.

from orbit.core.orbit_utils.bunch_utils_functions import copyCoordsToInitCoordsAttr

will give

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'orbit.core.orbit_utils.bunch_utils_functions'; 'orbit.core.orbit_utils' is not a package

The import from module itself works, following is OK:

from orbit.core.orbit_utils import bunch_utils_functions
bunch_utils_functions.copyCoordsToInitCoordsAttr

Could be done following StackOverflow ?

juanfem commented 3 weeks ago

The way I understand C++ extension modules is that if you want to import that way, you need to declare the submodule as a separate extension module, instead of using PyModule_AddObject

But if you define a separate extension module, I think you can't do

from orbit.core.orbit_utils import bunch_utils_functions
bunch_utils_functions.copyCoordsToInitCoordsAttr()

unless you create a __init__.py under orbit/core/orbit_utils that does from . import copyCoordsToInitCoordsAttr

azukov commented 3 weeks ago

I think I tried adding 'package' constant as mentioned in the above StackOverflow post and it worked.

juanfem commented 3 weeks ago

ah, I haven't tried that. I'll check that solution