module alias in import statement

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Apr 5 03:26:56 EDT 2016


On Tuesday 05 April 2016 14:27, Rustom Mody wrote:

> On Tuesday, April 5, 2016 at 9:53:30 AM UTC+5:30, Chris Angelico wrote:
>> On Tue, Apr 5, 2016 at 2:08 PM, Rustom Mody  wrote:
>> >> 'import tk.ttk' looks for 'tk' in sys.modules, does not find it, looks
>> >> for a module named 'tk' on disk, does not find it, and says so.
>> >
>> > A well-known quote comes to mind:
>> >
>> > | There are only two hard things in Computer Science: cache
>> > | invalidation and naming things.
>> >
>> > eg. http://martinfowler.com/bliki/TwoHardThings.html
>> >
>> > particularly since this seems to be in both categories :-)
>> 
>> sys.modules isn't really a cache in that sense, though. The "hard
>> problem" of cache invalidation comes from the fundamental assumption
>> that a cache hit should be semantically identical to a cache miss;
> 
> Following looks like a cache miss to me (certainly did to the OP):
> 
> On Monday, April 4, 2016 at 9:01:41 PM UTC+5:30, ast wrote:
>> hello
>> 
>> >>> import tkinter as tk
>> >>> import tk.ttk as ttk
>> 
>> Traceback (most recent call last):
>>   File "<pyshell#3>", line 1, in <module>
>>     import tk.ttk as ttk
>> ImportError: No module named 'tk'


But that *miss* isn't cached -- if the OP then created a package called "tk" 
with a submodule called "ttk", and then re-ran the `import tk.ttk as ttk` 
line, the import subsystem would have picked up the newly created package 
and imported it.

I think that's the point Chris was trying to make.



-- 
Steve




More information about the Python-list mailing list