Search path for pyui file
It seems to me that the interpreter looks for the pyui file in the current working directory. This makes it impossible to invoke an UI program without changing directory if the program is not located in the current working path.
Is it possible to change Pythonista's behavior so that it search for the pyui file in the same directory where the main script resides? Or a search hierarchy would be even better.
I am developing a shell framework that would really benefit from above changes. It is now awkward that I have to "cd" before a UI program can be called.
Use an absolute path instead.
load_viewwill also take a full path, either an abspath or a relative path relative to the current directory.
In particular, note the pyui line.
You might initially be tempted to use
__file__to find the pathname, but this doesn't work when running from the editor, while inspect.currentframe does work.
class editmenuclass(ui.View): _lastinstance=None def did_load(self): # ..... setting up button actions went here type(self)._lastinstance=self # only want one instance, can skip loading if it exists @classmethod def load(cls): import os, inspect pyui= os.path.abspath(inspect.getfile(inspect.currentframe()))+'ui' if cls._lastinstance is None: cls._lastinstance = ui.load_view(pyui) return cls._lastinstance
Of key interest will be the
pyui=line above, which assumes that you are loading the pyui that corresponds with the script name. If you have other pyui's, you would split out the path to get your base folder.
Note my pyui specifies the custom class, such that load_view returns an instance of editmenuclass. In this case only one instance of editmenuclass ever made sense, so to run my class I'd use:
editmenu = editmenuclass.load()
to give me either the existing instance (if one has been created before), or load the view.
You are like the ultimate source of answers on the forum. Maybe just slightly after omz himself.
Though it is not exactly what I am after, it is an awesome alternative. Thanks again!
If you're loading a single pyui file paired to your script, you can use a plain
ui.load_view()without arguments to load the pyui file named like the script. Of course this doesn't work if you're working with multiple pyui files, and I have no idea what it does when called from an imported module.