Closed micramm closed 11 years ago
The from
statement is also not supported: https://github.com/jparise/python-reloader/issues/14#issuecomment-10410461
The root of this problem appears to be caused by the inconsistent module names that we're intercepting in our __import__
override. Sometimes, we see partial (relative) names, and other times we get the full (absolute) names. I haven't found an easy way to normalize those yet.
One approach might be to ditch the __import__
override and move to an imports hooks-based system instead. I'm open to suggestions.
The top answer from http://stackoverflow.com/questions/211100/pythons-import-doesnt-work-as-expected should be useful. It would resolve the issue for all relative imports without the from
statement.
To support the from
statement, one has to keep track of the fromlist
and then re-execute the from
statement when reloading. There is some reference to this in the reload
documentation. http://docs.python.org/2/library/functions.html#reload
Thanks, @micramm! That was a very helpful reference. I'm not sure how I missed that myself.
I still need to work on fromlist
support, but I'll track that separately.
It seems that intra-package imports may not currently get reloaded. If we have a package with the folder structure given in documentation example http://docs.python.org/2/tutorial/modules.html#packages
and we do in
main
:where in
echo
there is an intra-package import:Then
surround
will not get reloaded: the name of the imported package will be surround butsys.modules.get('surround', None)
will returnNone
becausesys.modules
will contain only the full referencesounds.effects.surround
.I think in case of such imports the only way to get the module is to use the return value of
_baseimport
. As in your comment, this would not work forimport package.module
, but not sure how to easily distinguish the two cases.