jparise / python-reloader

Dependency-based Python Module Reloader
http://www.indelible.org/ink/python-reloading/
MIT License
135 stars 27 forks source link

Intra-package importing #15

Closed micramm closed 11 years ago

micramm commented 11 years ago

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:

import reloader
reloader.enable()
import sound.effects.echo as m
reloader.reload(m)

where in echo there is an intra-package import:

import surround

Then surround will not get reloaded: the name of the imported package will be surround but sys.modules.get('surround', None) will return None because sys.modules will contain only the full reference sounds.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 for import package.module, but not sure how to easily distinguish the two cases.

Vanuan commented 11 years ago

The from statement is also not supported: https://github.com/jparise/python-reloader/issues/14#issuecomment-10410461

jparise commented 11 years ago

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.

micramm commented 11 years ago

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

jparise commented 11 years ago

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.