Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106469

Re: module alias in import statement

From Terry Reedy <tjreedy@udel.edu>
Newsgroups comp.lang.python
Subject Re: module alias in import statement
Date 2016-04-04 16:46 -0400
Message-ID <mailman.38.1459802831.32530.python-list@python.org> (permalink)
References <570288d0$0$19758$426a74cc@news.free.fr> <ndujs3$7n6$1@ger.gmane.org>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

module alias in import statement "ast" <nomail@com.invalid> - 2016-04-04 17:31 +0200
  Re: module alias in import statement Ned Batchelder <ned@nedbatchelder.com> - 2016-04-04 08:59 -0700
  Re: module alias in import statement Steven D'Aprano <steve@pearwood.info> - 2016-04-05 02:15 +1000
  Re: module alias in import statement Terry Reedy <tjreedy@udel.edu> - 2016-04-04 16:46 -0400
    Re: module alias in import statement Rustom Mody <rustompmody@gmail.com> - 2016-04-04 21:08 -0700
      Re: module alias in import statement Chris Angelico <rosuav@gmail.com> - 2016-04-05 14:23 +1000
        Re: module alias in import statement Rustom Mody <rustompmody@gmail.com> - 2016-04-04 21:27 -0700
          Re: module alias in import statement Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-04-05 17:26 +1000
            Re: module alias in import statement Chris Angelico <rosuav@gmail.com> - 2016-04-05 17:47 +1000

csiph-web