I'm working within Maya. What i need is to import a custom package and completely reload it inlcuding all subpackages and modules. Mainly because i want Maya autocompletes for me the module path while im writing. For example i have this kind of package structure
root (package)
subpackage1 (package)
__init__.py (module)
a.py (module)
b.py (module)
subpackage2 (package)
c.py (module)
d.py (module)
e.py (module)
What i did on __init__.py files is to import all submodules and one by one to reload them there
ex: root.subpackage1.__init__.py
import a
import b
reload(a)
reload(b)
This is very tedious, and thus i suppose there is a much better way to import and reload all the structure
When i import the package the first time the whole structure is load into memory.
import root
reload(root)
root.subpackage1.a.my_function()
so lets suppose now i update a.py file and add a function named my_test. I need again to reload all package structure to be able to evaluate it like this.
root.subpackage1.a.my_test()
whitout doing this...
from root.subpackage import a
reload(a)
a.my_test()
I wall everything to be reloaded when i import and reload root whats the best practice to do this? Thanks in advance.
The closest thing I have found is directly based on this answer to a similar question (https://stackoverflow.com/a/1057534/7124215). I have introduced some changes for the __init__.py files of your question:
from os.path import dirname, basename, isfile
import glob
modules = glob.glob(dirname(__file__)+"/*.py")
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]
from . import *
for module in __all__:
reload(module)
del dirname, basename, isfile, glob, modules
Does this help? I apologize if I have not understood your question correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With