-
Notifications
You must be signed in to change notification settings - Fork 247
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
application_importer : provide folders to be included #2655
Comments
If I remember correctly, the plan was to have all python scripts for your application in What use did you have in mind for this new feature? |
Well it not a feature what I have in mind .. but more of organization of the python scripts into different folders solvers, processes, utilities(may be) , trilinos and so on depending on application .. ! all of them can be a different folder inside 'python_scripts' folder or outside and can these be added to the path depending on what a application specifically wants |
I fully agree with having subfolders in One thing we were discussing with @oberbichler and @philbucher is that it would be good if applications behaved as native python modules, which would allow both a subfolder structure and a submodule organization in python (I am thinking I have some ideas about how all this could be implemented without destroying backwards compatibility, but at this point this is more of a wish than a planned feature. |
What you are describing has also the same effect as I was hoping. |
Just for future reference: #2848 is also caused by the hackish handling of application modules, and whatever changes we propose in the future should be designed to solve it. |
I was thinking of something like this in application_importer at line
import sys
Then each sub folder in python_scripts is no longer a module but is just on he python path. |
The following code works for me
The idea is to add the contents of sub folders of 'python_scripts' to the Application module. I am also adding a zip file with an example |
Does this (imp) work with python2? |
I tested it with python 2.7 .. it works .. ! |
Minimal modification for supporting the subfolder names class Module(object): # << new
pass
# walking in python_scripts folder. i.e iterating over the subfolders and files recursively
for dir_name, subdir_list, file_list in os.walk(python_scripts_path):
# Getting the directory name without its path
dir_name_wo_path = os.path.basename(dir_name)
# we do not want to look inside __pycache__ folder
if (dir_name_wo_path == '__pycache__'):
continue
module = Module() # << new
setattr(current_app_module, dir_name_wo_path, module) # << new
# looping over files in this folder
for file_name in file_list:
# obtaining the extension of the file
file_extension = os.path.splitext(file_name)[1]
# we do not need .pyc files
if (file_extension == '.pyc'):
continue
# name of the current file with full path. needed for importing.
file_name_with_path = os.path.join(dir_name, file_name)
# (just) the file name with out .py
file_module_name = os.path.splitext(file_name)[0]
# importing the file module
file_module = imp.load_source(file_module_name, file_name_with_path)
# setting the file module into the Application module
setattr(module, file_module_name, file_module) # << new Then you can do: But I am not sure if this will work for all possible scenarios... |
Yes that is a nice idea .. I tested it .. it works with both python 2.7 and 3.4 |
@oberbichler |
Another question: |
@adityaghantasala I would say the scenarios we do not expect. Users can do crazy stuff with python. But I think we need some limitations and can not support all python functionalities since we are using a 'hacky' solution. |
@philbucher this should work as an alternative to the modules. To be honest I would prefer a "native" solution since, as @oberbichler is saying, we never know when this might fail to reproduce the behavior of a real module. I am now looking for alternative ways to combine pybind and native python in a single module but this is an improvement over the current situation, so I think we should consider it. |
@jcotela a solution involving pybind means that in the configure file (or somehow) we should include the (sub)folders which will be treated as modules ? or can it be automated to take all the subfolders in python_scripts to make them modules ? |
In my opinion the trick is to point the application to the |
I personally like a solution without adding |
I understand the reasons for a custom solution but in general I would prefer the |
my only concern about having init.py is maintenance. Also as soon as init.py is there people can do user/problem/app specific things which can break testing and may be other thing .. ! If it should be a empty (and is somehow forced to be empty) yes I am totally for it. Also in this case I think some automation can be done to create these files (which excludes possibility of user/problem/app specific code in it) ... ! |
Why? |
imagine someone puts |
imagine someone puts |
yes .. it can very well happen .. |
I agree with @adityaghantasala in the sense it is better to keep things simple |
Well, in my view the main argument for adopting the |
@jcotela can we use the standard way without major changes? |
No, but we can leave the "adding everything to path" active during the transition period (or that is my hope at least). |
Hm that also sounds good +1 for adding the init.py then |
@adityaghantasala in contrast to your solution, have you tried to make Kratos to use the regular python way of importing submodules? I was discussing this with @jcotela and @oberbichler the other day and we thought this might be a better way to go for the future We should discuss it before proceeding with this |
this means having init.py if I am correct ? |
The |
solved properly in #3217 |
Currently the application_importer always adds "python_scripts" and "constitutive_laws" to the python paths. These are hard coded.
Is there a specific reason to do this ? If not can a list of folders be passed by the ****Application.py so that all of them are added to the path ?
The text was updated successfully, but these errors were encountered: