Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.075 X-Spam-Evidence: '*H*': 0.85; '*S*': 0.00; 'subject:file': 0.07; 'sys': 0.07; 'imported': 0.09; 'okay': 0.09; '(either': 0.16; '24,': 0.16; 'accepts': 0.16; 'did,': 0.16; 'filesystem': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'module?': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'module': 0.19; 'mechanism': 0.19; 'import': 0.22; 'exists': 0.24; 'script.': 0.24; 'skip:e 30': 0.24; 'file.': 0.24; 'purposes': 0.26; 'this:': 0.26; 'asking': 0.27; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'getting': 0.31; '"",': 0.31; 'mod': 0.31; 'names.': 0.31; 'stuff': 0.32; 'implemented': 0.33; 'maybe': 0.34; 'could': 0.34; 'problem': 0.35; 'something': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'surely': 0.36; 'doing': 0.36; 'should': 0.36; 'too': 0.37; 'two': 0.37; 'problems': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'that,': 0.38; 'anything': 0.39; 'recent': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'ian': 0.60; 'solve': 0.60; 'details.': 0.61; 'name': 0.63; 'different': 0.65; 'believe': 0.68; 'design.': 0.68; 'hoping': 0.75; 'power': 0.76; '7:00': 0.84; 'bare': 0.84; 'imp': 0.84; 'much,': 0.84; 'presumably': 0.84; 'replicate': 0.84; 'magical': 0.91; 'that),': 0.91; '2013': 0.98 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:to :content-type; bh=bb6Rmv4aThOv9P8B011HA8BhLVnuprf9jUzkFcukroI=; b=VawnEB5WSz3sn0SZXk3JBw+jFjfJajFJrGedDKhmanUAkoweCxMZ/rnasMNGe227ii PAc6BcyrC7Mg2BL8EOBqYYF+UVD8CSF3oDvo7qS72x9WTRUN3z5R6o8fvj6k4Nmnxumk NcPkrmy4kdzQDXfC6mBzeLyfjSNXdgsUsVEyidVjsar+m+/hM5esqv3PHj1IgkQOQIii DucSeAsuwgV5XLxlsw+p1glgzMmgBZLA4K8FKP6gcsvdrRij3jDvHbj2qa2oxvNj/EdU X0mgY094pqCfm6sHYtpVc1yV8I8w3/mqcifsu1g73B39eWMeqJigCVRN58LgFhquqo93 aAGQ== MIME-Version: 1.0 X-Received: by 10.66.218.166 with SMTP id ph6mr21565867pac.28.1385280458658; Sun, 24 Nov 2013 00:07:38 -0800 (PST) In-Reply-To: References: Date: Sun, 24 Nov 2013 19:07:38 +1100 Subject: Re: Importing by file name From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385280468 news.xs4all.nl 15915 [2001:888:2000:d::a6]:51220 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60354 On Sun, Nov 24, 2013 at 7:00 PM, Ian Kelly wrote: > The importer mechanism as far as I know only accepts module names, not > filesystem paths; I believe this is by design. You could imitate it by > doing something like this: > > import imp > import sys > > mod = imp.new_module('spam') > exec(open('/path/to/spam.py').read(), mod.__dict__) > sys.modules['spam'] = mod That's a bit better than just working with a bare dictionary as I did, but it's still getting a bit heavy-handed with the details. How is __main__ imported? Presumably that one, at least, can be loaded from a specific file. Or is that pure magic? > Is 'spam' really the appropriate name for this module? What if there is > already a different module with the name 'spam'? Presumably if the module > is named 'spam' then it should be importable as 'spam', but then why the > need for the path-based import? Alternatively, you might name the module > something like "", which surely won't collide with > anything imported by the normal mechanism, but then what if the spam module > is also imported by normal means? You would end up with two copies of the > same module with different names. The same problem already exists with __main__ - I was hoping to replicate that, not necessarily to solve all its problems :) I'm okay with using a fixed name (either "__main__" or something that doesn't collide with that), for the purposes of experimentation. I know the recent Pythons give a lot of import power to the script. But maybe I'm just asking too much, and some of this stuff really is magical and implemented in C? ChrisA