module alias in import statement
Terry Reedy
tjreedy at udel.edu
Mon Apr 4 16:46:52 EDT 2016
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.
--
Terry Jan Reedy
More information about the Python-list
mailing list