What is the recommended/ideal way to do python-to-python imports? #233
-
|
Importing a python file to a mast file is pretty straightforward: I've figured out that this works: Is there a better way? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
|
It is highly recommended to only import python files in the same folder to allow the folder to be bundled as an add on. (Note: I haven't tried any of the following. but it would be the intended method) This may require a _init_.py which can be empty for python to recognize it or the python file may need to be ordered correctly in the _init_.mast. If this is in a standalone mission that is not intended to use folders as packaged addons, then you may have the python in a folder. Make sure the folder has a _init_.py (instead of or including a _init_.mast With this is in place it may work to: If you intend this to be part of a module/addon for use in other missions it should be packaged as a sbslib and dependent missions should place it in the proper section in story.json. The folder could be zipped up into a file and use the sbslib extension. An sbslib should have no MAST files. |
Beta Was this translation helpful? Give feedback.
-
|
The normal
Condition 3 is what tripped me up. If you have code that need to be referenced by multiple other files in folders you want to keep as independent add-ons, |
Beta Was this translation helpful? Give feedback.
-
|
Yes, as the marked answer describes If the python file is imported in _init_.mast it will be a python module which is why and how the answer works internally. |
Beta Was this translation helpful? Give feedback.
The normal
import file_nameandfrom file_name import function_namesyntax will work as long as:file_name.pyis in the same folder as the file you're adding the import statement to__init__.mast__init__.mast,file_name.pyis listed prior to the the file you're adding the import statement toCondition 3 is what tripped me up.
If you have code that need to be referenced by multiple other files in folders you want to keep as independent add-ons,
it's recommended to put the shared code in an sbslib (or mastlib) that they can reference.Edit: I disagree with this recommendation; see below comment