Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed0.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Reason for not allowing import twice but allowing reload() Date: Mon, 29 Feb 2016 18:01:30 +1100 Lines: 68 Message-ID: References: <645cdd46-d4a4-49b3-a0d8-848608d70d73@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de mE/RDcFTxNy2Xt4rFf2FSgaf+I7PdDGWRZ/gEb4ru1Sw== 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; 'received:209.85.223': 0.03; '(even': 0.05; 'newbie': 0.05; 'sys': 0.05; '(using': 0.07; 'assignment': 0.07; 'difference,': 0.07; 'cc:addr:python-list': 0.09; 'effect.': 0.09; 'implemented,': 0.09; 'imports': 0.09; 'internally': 0.09; 'normally,': 0.09; 'pause': 0.09; 'subject:()': 0.09; 'subtle': 0.09; 'sys.stdout': 0.09; 'warn': 0.09; 'python': 0.10; 'subject:not': 0.11; 'importing': 0.15; 'subsequent': 0.15; '(massively': 0.16; '2016': 0.16; 'assignment.': 0.16; 'does;': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'operation.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reimport': 0.16; 'statement)': 0.16; 'subject:import': 0.16; 'sys.path': 0.16; 'two,': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'instance,': 0.18; 'module,': 0.18; 'numerical': 0.18; '>>>': 0.20; 'load': 0.20; 'library': 0.20; 'versions': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'otherwise,': 0.20; 'fairly': 0.22; 'object.': 0.22; 'visible': 0.22; 'trying': 0.22; 'code,': 0.23; 'feb': 0.23; 'third-party': 0.23; 'second': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'module': 0.25; "i've": 0.25; 'error': 0.27; 'bugs': 0.27; 'message- id:@mail.gmail.com': 0.27; 'disk': 0.27; 'module.': 0.27; 'regular': 0.29; 'behind.': 0.29; 'does,': 0.29; 'loads': 0.29; 'that.': 0.30; 'everyone': 0.31; "can't": 0.32; 'possibly': 0.32; 'though,': 0.32; 'maybe': 0.33; 'run': 0.33; 'changing': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'could': 0.35; 'important.': 0.35; "isn't": 0.35; 'sometimes': 0.35; 'step': 0.36; 'but': 0.36; 'should': 0.36; 'project': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'modules': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; "won't": 0.38; 'received:209': 0.38; 'several': 0.38; 'why': 0.39; 'test': 0.39; 'sure': 0.39; 'does': 0.39; 'takes': 0.39; 'rather': 0.39; 'your': 0.60; 'skip:n 10': 0.62; 'making': 0.62; 'strange': 0.63; 'more': 0.63; 'kindly': 0.64; 'believe': 0.66; 'fact,': 0.67; 'production': 0.67; 'you:': 0.79; 'chrisa': 0.84; 'ever.': 0.84; 'total,': 0.84; 'absolutely': 0.88; 'to:none': 0.91; 'hundred': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=nenX7QVxFmotth4bUduSqaABZm1VaB3Zrq1ZgXE/8j0=; b=H/s0wOKTlxhIl/jp6ZYLiESTGAarOR+07dUU3RBAXCeMysT0e7wxTzauonsngsjWcW vEL+ipcEbvqxWa/1FINcipHeOu/8jLRDO/i7S7alxr9BmzpZ9D8LmRsK4G08i2Zwr32r RrvrMW1uWt44TS3Ezbd2qUFWwzljczUFzTXK0aCakUs+excCzRTDgySfA+bYgZkYMCkF oUNtd9pPucHzsEZug6N6qNV8KtoK7ekQ60dzeohbj4OOsAm0XwIHyPSugVAKzqQTsBFT /AZiDl/fhC9T1AHj04q8li3P2cyZCHfCYo1xRZOUDAKBi17xoC/UzpnV/c7lkFqqHKfM wWew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=nenX7QVxFmotth4bUduSqaABZm1VaB3Zrq1ZgXE/8j0=; b=EX00N04EeJavI8yK8974DyR+Udx1i1/+Dl4ErWMFj19cCbcpsbh+bwpfDD9qlIXa3l cFWh4PXcQiNXBZ9uCeO4tfb8jtEyznaZZmpD4MaYjLCgEnLEQ0NryKwZ958E97drnBIH 3GQeLf9GZr+4XnymE5qb2fSPnBTCeAP/CzXkclINdOkmcOkUmWxOvVlDh/9IvBzNY/Fi ArJ7E7QKRy1yqO3DXpKcS2eWPdlQwwjNPBQCddxdQgCb9uamrojLtpN+khqFuOUqQxJL 9Me/M89JI6RNdd5zzZkDENSkaXAhrxPE4t6XPRCDpcdjxCodK+/tsbMwGSduxbblATW2 jjhg== X-Gm-Message-State: AD7BkJLJJJjOLyBvcL5NxcuAaVZM08CHqEM/xo63u4dBlkRELjRu3+cHYokLBrPedAL0wxpUE4iD/UlJiL/uyg== X-Received: by 10.107.63.137 with SMTP id m131mr6771491ioa.157.1456729290280; Sun, 28 Feb 2016 23:01:30 -0800 (PST) In-Reply-To: <645cdd46-d4a4-49b3-a0d8-848608d70d73@googlegroups.com> 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: , Xref: csiph.com comp.lang.python:103687 On Mon, Feb 29, 2016 at 5:40 PM, wrote: > We can not import a module twice in a session of Python (subsequent attempts to import same module don't result in any error though, but it is not-effective). > > However after making change to module, we can reload() it (if not reload(), we could possibly have reimport statement) to get the updates in module. > > I am a newbie in Python (learning via IDLE) and trying to understand > > - what is extra (special) that import does, that reload() doesn't? > - if both do the same internally and externally, why subsequent imports of same module are ineffective? > - if subsequent imports of same module in a session are not effective, why not simply flag those attempts as an error, rather than letting them go effect-less. > > Kindly help me understand the reasons behind. Normally, you import a module because you want its features. For instance, you can "import re" to get access to the Regular Expression parser, or "import pandas" to load up the rather hefty third-party numerical computation/analysis library of that name. Sometimes, this can take a lot of work - importing pandas loads several hundred modules in total, and can take a second or two, if it's not already in the OS's disk cache. (Even if it is in cache, it takes a visible pause - maybe 100-300 ms.) If two parts of your project need the same module, it's way more efficient to make use of the already-loaded module. Additionally, sometimes it makes a lot of difference, and you _want_ to use the same module. When you "import sys", you absolutely must access the same sys module that everyone else does; otherwise, changing sys.stdout or sys.path wouldn't have any effect. And depending on how it's implemented, sys.intern could create very subtle bugs if you had two versions of it around. So here's what happens (massively oversimplified) when you "import modulename": 1) Is modulename in sys.modules? If so, return that. 2) Step through sys.path, looking for the module. Can't find it? ImportError. 3) Run that program and create a module object. Put it into sys.modules, and return it. You can test that first step fairly easily: >>> sys.modules["asdf"]="Haha" >>> import asdf >>> asdf 'Haha' As a general rule, this is what you want (apart from sticking strings into sys.modules, which is a VERY strange thing to do!). In fact, I would recommend never using reload() unless you're absolutely sure of what it does and how it's important. I've never used reload() in any production code, ever. Ever. The reason that repeating the import isn't flagged as an error is that it's simply an assignment. If you write this code, Python won't warn you: >>> os = sys.modules["os"] >>> os = sys.modules["os"] >>> os = sys.modules["os"] >>> os = sys.modules["os"] Yet this is effectively what "import os" does. It's assignment, just like any other assignment operation. I believe it is theoretically possible to ask Python to give you a warning (using an import hook), but generally, you should think about imports simply as assignment. ChrisA