Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: module alias in import statement Date: Mon, 4 Apr 2016 16:46:52 -0400 Lines: 38 Message-ID: References: <570288d0$0$19758$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 4wfXQJQov3uS+4gPgNrm/wMEZr27HpbCHUf0i3AFgIcw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'importerror:': 0.05; 'sys': 0.05; 'ast': 0.09; 'imports': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:module': 0.09; 'jan': 0.11; 'importing': 0.15; 'caches': 0.16; 'modules,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'strange,': 0.16; 'subject:import': 0.16; 'sys.modules': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'so.': 0.22; '"",': 0.22; 'tkinter': 0.22; 'am,': 0.23; 'import': 0.24; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'said,': 0.27; 'looks': 0.29; 'says': 0.32; 'traceback': 0.33; 'file': 0.34; 'false': 0.35; "isn't": 0.35; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'does': 0.39; 'to:addr:python.org': 0.40; 'real': 0.62; 'course': 0.62; 'received:96': 0.63; 'ttk': 0.84; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-96-227-207-81.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.1 In-Reply-To: <570288d0$0$19758$426a74cc@news.free.fr> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <570288d0$0$19758$426a74cc@news.free.fr> Xref: csiph.com comp.lang.python:106469 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 "", line 1, in > 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