In the Python controller, I can type in a directory name with a dot that then follows the module name. This goes like an example: example.someModule.Func
But if I type any directory name that precedes this example with the dot included, UPBGE reads it as a module, not a directory. Am I missing something here? I want to get UPBGE to read the directory set before the example as a directory, and then read the sub-directory as a sub-directory and import the module from there.
Which in theory, should import the example module mentioned here, but reads the directory portion as just a module (beginning part). How can I solve this and load a module from one directory back and a sub-folder, or a different directory altogether?
Sorry if I might be a little repetitive in this post, but I need a solution to this issue.
import FileA # don use .py in end python file in import
FileA.Main() # or FileA.name_you_function_in_python_fileA()
Treat it like a module: import file. This is good because it is safe, fast and easy to maintain. The code is reused the way it should be done. Most Python libraries work using multiple methods, stretched over many files. Highly recommended. Note that if your file calling file.py . import on should not include .the expansion is at the end.
The infamous (and insecure) exec command: insecure, hacker-like, usually the wrong answer. Avoid where possible.
execfile('file.py ') in Python 2
exec(open('file.py ').read()) in Python 3
I tried to use the import module in tandem with a game property with expandPath, but it raises a ModuleNotFoundError. I want to make it import through a game property that defines the path, and another game property defines what script file to use, like a pointer.
The goal is to:
Use a game property to define the directory path
Use another game property to point to the script file (example.main) as a sample, and then load it through the import module.
But this just raises the ModuleNotFoundError. How to import a script via expandPath and game properties?
you need concatenation of the strings - just sum strings
own[âpath_scriptsâ]
own[âscript_nameâ]
path = expandPath(â//â+own[âpath_scriptsâ]+â/â+own[âscript_nameâ])
if in you path or names folders use numbers (path/folder1/folder2) - use convert numbers to string - expandPath(â//folderâ+str(1)), if you need insert name script in run function - assign name to string variable and translate to method -
string = own[âfunction_nameâ] #string global variable
fileA.string()