bwesterb / py-demandimport

Lazy import python modules for low start-up time. (Taken from mercurial.)
GNU General Public License v2.0
46 stars 8 forks source link

Import order seems significant #7

Open spacebuoy opened 6 years ago

spacebuoy commented 6 years ago

Great tool but I'm seeing some unexpected results compared to what I think I read in the description.

Running on python versions 3.5.2 and 3.6.4.

Module code where os.path is loaded before loading os: import os.path as pp print( pp) import os print('os) Output: mod <module 'posixpath' from '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/posixpath.py'> mod <unloaded module 'os'>

Above, os.path seems to have executed, yes? os remains unloaded.

Module code where os is loaded before loading os.path: import os print('mod', os) import os.path as pp print('mod', pp) Output: mod <unloaded module 'os'> mod <module 'posixpath' from '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/posixpath.py'> mod <module 'os' from '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/os.py'>

Both os and os.path have executed after importing os.path.

mod 2 <module 'posixpath' from '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/posixpath.py'> mod 2 <unloaded module 'os'>

The first module imports mod 2 where os remains unloaded. Does demandimport keep imports separate between modules?

I hope you can have a look. Thanks

bwesterb commented 6 years ago

Yeah, demandimport doesn't act perfectly in every situation. Do you run into a fatal problem?

By the way, demandimport can tell you a lot more about what it is doing as follows:

def log(fmt, *args):
    print fmt % args
demandimport.set_logfunc(log)
demandimport.enable()

# import ...