Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

making python-modules work #3217

Merged
merged 10 commits into from
Dec 3, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,6 @@ install(TARGETS KratosStructuralMechanicsCore DESTINATION libs )
install(TARGETS KratosStructuralMechanicsApplication DESTINATION libs )

# Add to the KratosMultiphisics Python module
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/StructuralMechanicsApplication.py" DESTINATION KratosMultiphysics )
install(DIRECTORY DESTINATION "KratosMultiphysics/StructuralMechanicsApplication")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this works in Win (using / for separating the path)
=> if not I think it can be easily fixed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually you are safe with "/". It is also a valid separator for windows.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is not any longer in

KratosMultiphysics/applications/StructuralMechanicsApplication?

this means that you can only make installation outside of where you compile?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t understand, can you please explain in more detail what you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right now on my computer i normally keep the python files in the same directory as they normally are. This is useful since when i change a file i do not need to reinstall.

I am asking if this will change after this PR or if the python files will be left where they are

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clarifying on my point, it may even be good if this is changed, since it may be the first step towards propoer python packaging of Kratos...however the need of installing every time a python file is modified is a little annoying, which is the reason for which i ask

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah now I understand
Answer: the current behavior is NOT changed, i.e. you DON'T have to reinstall after changing python files
Only the folder-structure changes a bit in the KratosMultiphysics folder
from:

KratosMultiphysics
    - __init__.py
    - StructuralMechanicsApplication.py
    - FluidDynamicsApplication.py

to:

KratosMultiphysics
    - __init__.py
    - StructuralMechanicsApplication
        __init__.py # this is the "StructuralMechanics.py"-file
    - FluidDynamicsApplication.py

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, i see.

thx!


install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/StructuralMechanicsApplication.py" DESTINATION "KratosMultiphysics/StructuralMechanicsApplication" RENAME "__init__.py")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, I am not sure if this works in Win (using / for separating the path)
=> if not I think it can be easily fixed

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
application_folder = "StructuralMechanicsApplication"

# The following lines are common for all applications
from . import application_importer
from .. import application_importer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this also works in python2
=> if not I think it can be fixed with little effort

import inspect
caller = inspect.stack()[1] # Information about the file that imported this, to check for unexpected imports
application_importer.ImportApplication(application,application_name,application_folder,caller)
application_importer.ImportApplication(application,application_name,application_folder,caller, __path__)
7 changes: 4 additions & 3 deletions kratos/python_interface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# Initialize Kernel so that core variables have an assigned Key even if we are not importing applications
KratosGlobals.Kernel.Initialize()

__path__.append(KratosLoader.kratos_scripts)

def CheckForPreviousImport():

first_caller = KratosGlobals.AuthorizedCaller[
Expand Down Expand Up @@ -48,6 +50,5 @@ def CheckForPreviousImport():
def CheckRegisteredApplications(*applications):
for app in applications:
if not KratosGlobals.Kernel.IsImported(app):
import __main__
raise Exception("Application "+ app + " was not imported in the main script ("+__main__.__file__+")")

import __main__
raise Exception("Application "+ app + " was not imported in the main script ("+__main__.__file__+")")
6 changes: 5 additions & 1 deletion kratos/python_interface/application_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from KratosMultiphysics import Logger


def ImportApplication(application, application_name, application_folder, caller):
def ImportApplication(application, application_name, application_folder, caller, mod_path=None):
Globals = KratosMultiphysics.KratosGlobals
Kernel = Globals.Kernel
main_caller = Globals.AuthorizedCaller
Expand All @@ -30,6 +30,10 @@ def ImportApplication(application, application_name, application_folder, caller)
# Add constitutive laws python scrips folder to path
constitutive_laws_path = os.path.join(python_path, 'constitutive_laws')
sys.path.append(constitutive_laws_path)

if mod_path is not None: # optional for backwards compatibility
mod_path.append(python_path)

# Add application to kernel
Kernel.ImportApplication(application)
# Dynamic renumbering of variables to ensure consistency
Expand Down