Skip to content

Standard way of importing python-modules #5402

@philbucher

Description

@philbucher

With the "old" way of importing applications being deprecated, we now should find a common solution for importing the python scripts
Previously we had to do:

import KratosMultiphysics.FluidDynamicsApplication
import vms_solver

now we have several options:

import KratosMultiphysics.FluidDynamicsApplication.vms_solver as vms_solver
from KratosMultiphysics.FluidDynamicsApplication import vms_solver

However, what is also possible and where we had several discussions (e.g. here and here) are explicit relative imports:

# in vms_sovler.py:
from KratosMultiphysics.FluidDynamicsApplication import fluid_solver # absolute import
# OR:
from . import fluid_solver # explicit relative import

from PEP8:
"Absolute imports are recommended, as they are usually more readable and tend to be better behaved ..."

"However, explicit relative imports are an acceptable alternative to absolute imports, especially when dealing with complex package layouts where using absolute imports would be unnecessarily verbose"

I tried both ways already and suggest (up for discussion the following):

  • By default I would go for absolute imports, just as PEP8 suggests.
  • Since however Kratos has a relatively large/complex file structure I would also allow explicit relative imports, if the developer decides to use it, since it does shorten the paths quite a bit (e.g. here)
    => especially when there is a subfolder-structure inside python_scripts (which is now possible and already in use, see CoSimApp and HDF5App) since it can make the paths much shorter
  • What I would not allow is to use explicit relative imports across directories (e.g. importing from parent directory etc), bcs this becomes really unreadable then example

FYI @armingeiser @adityaghantasala

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions