module alias in import statement
Rustom Mody
rustompmody at gmail.com
Tue Apr 5 00:08:32 EDT 2016
On Tuesday, April 5, 2016 at 2:17:24 AM UTC+5:30, Terry Reedy wrote:
> On 4/4/2016 11:31 AM, 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'
> >
> >
> > of course
> >
> >>>> import tkinter.ttk as ttk
> >
> > works
> >
> > Strange, isn't it ?
>
> Nope. As other said, 'import tkinter as tk' imports a module named
> 'tkinter' and *in the importing modules, and only in the importing
> module*, binds the module to 'tk'. It also caches the module in
> sys.modules under its real name, 'tkinter'.
>
> >>> import tkinter as tk
> >>> import sys
> >>> 'tkinter' in sys.modules
> True
> >>> 'tk' in sys.modules
> False
>
> '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 :-)
More information about the Python-list
mailing list