Brett Cannon added the comment:

It actually is a caching issue, but not with the caches in the finder but the 
cache *of* finders. Because you inserted LIBDIR before it existed, import 
noticed it didn't exist and so put None into sys.path_importer_cache[LIBDIR] 
(or imp.NullImporter prior to Python 3.3). If you del 
sys.path_importer_cache[LIBDIR] just before trying to import impfile then it 
works. If you leave the directory around but clear out its contents then 
importlib.invalidate_caches() would have been needed.

As you have noticed, dynamically mucking around import is rather delicate. 
There are various caches and tricks used in order to speed it up since it is 
such a common operation. If you are trying to just load a single file that you 
dynamically wrote you can load the file directly using 
http://docs.python.org/3/library/importlib.html#importlib.machinery.SourceFileLoader
 (or if you need to support Python 2.7 as well, 
http://docs.python.org/2.7/library/imp.html#imp.load_module).

Do let me know if you are trying to just load a single file. I'm contemplating 
adding a utility function to help with that use-case in importlib.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue20321>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to