I have a package with a similar structure as scipy/numpy, where you have a few main submodules, and each submodule contains functions from various files that have been flattened into the same namespace. Let’s say
package/
sub1/
__init__.py
file1.py
file2.py
And then sub1/__init__.py looks like this:
from .file1 import func1, func2
from .file2 import func3
The result is that I can do
import package.sub1
package.sub1.func1()
However, the problem is that the following does not work:
>>> import package.sub1
-- change things in file1.py --
>>> reload(package.sub1)
The function does not update. It works if I do import package.sub1.file1 instead, so there is something with flattening the namespace, that makes it lose the connection to the module. My main question is how I can still use the reload command, while still getting the benefits of being able to write sibling functions in separate files.
Make the
__init__.pyreload the files it imports when it is imported:That said, relying on
reload()is generally a bad idea if you can avoid it.