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


Groups > comp.lang.python > #60353

Re: Importing by file name

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'interpreter': 0.05; 'importerror:': 0.07; 'subject:file': 0.07; 'sys': 0.07; 'imported': 0.09; 'raises': 0.09; 'python': 0.11; '23,': 0.16; 'accepts': 0.16; 'contents:': 0.16; 'filesystem': 0.16; 'happily': 0.16; 'knock': 0.16; 'module?': 0.16; 'pathname': 0.16; 'switch.': 0.16; 'syntaxerror:': 0.16; 'world!': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'module': 0.19; 'mechanism': 0.19; '>>>': 0.22; 'import': 0.22; 'email addr:gmail.com&gt;': 0.22; '&gt;&gt;&gt;': 0.24; "aren't": 0.24; 'script.': 0.24; 'skip:e 30': 0.24; 'skip:i 40': 0.24; 'helpful': 0.24; "i've": 0.25; 'script': 0.25; 'this:': 0.26; 'post': 0.26; 'skip:_ 20': 0.27; 'header:In-Reply-To:1': 0.27; 'absolute': 0.30; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'easier': 0.31; '"",': 0.31; 'mod': 0.31; 'names.': 0.31; 'file': 0.32; 'skip:& 30': 0.33; 'could': 0.34; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; 'surely': 0.36; 'done': 0.36; 'doing': 0.36; 'should': 0.36; 'two': 0.37; 'being': 0.38; 'skip:& 10': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'anything': 0.39; 'success.': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'manually': 0.60; 'name': 0.63; 'different': 0.65; 'believe': 0.68; 'design.': 0.68; 'invalid': 0.68; 'how.': 0.84; 'imp': 0.84; 'presumably': 0.84; '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:from:date:message-id:subject:to :content-type; bh=g1VpSxUBBOtPcP3GFLITAx5S17/59kmFSs1C+rOgO/0=; b=Jv9pJI9lTiPAd+IAURXHuacgy7efbPJvpeBkxtupBKqSgaxpuqBupteDs2T4gPfN3D FiM0YVwWLw4g+cXqhZmYrtc3CvL7OCWKYwcTw5zQBoEf1NLT9Dlagejwc5XUH2R8B25v cfblsh0SKypVYrebo4LAXOSJDStMtzVEJwhS3SYd552mUtRFCkZ+8sTBl10D6lU2CLtk BxzelWBJAPuUGSm+QYxBznFY4PkeOdJqS//SmbK+Rbeolpw4wng2rmmRH96Term/h0bK oM4q1JgdPVg4+e6nAw4/e/dGIUgK/GslYexkShr+F24+k+2NwgtrrN0cN1GkbVEWdNys DTeQ==
X-Received by 10.68.232.3 with SMTP id tk3mr1561483pbc.121.1385280050226; Sun, 24 Nov 2013 00:00:50 -0800 (PST)
MIME-Version 1.0
In-Reply-To <CAPTjJmqhiyT9zVHFRyKH0UwhdhFgZxA1rcbarChePMyfK7dDTA@mail.gmail.com>
References <CAPTjJmqhiyT9zVHFRyKH0UwhdhFgZxA1rcbarChePMyfK7dDTA@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 24 Nov 2013 01:00:10 -0700
Subject Re: Importing by file name
To Python <python-list@python.org>
Content-Type multipart/alternative; boundary=047d7b33dbf24eeb9304ebe7a5d4
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3118.1385280053.18130.python-list@python.org> (permalink)
Lines 121
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385280053 news.xs4all.nl 15963 [2001:888:2000:d::a6]:48005
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60353

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Nov 23, 2013 9:42 PM, "Chris Angelico" <rosuav@gmail.com> wrote:

> As part of a post on python-ideas, I wanted to knock together a quick
> little script that "imports" a file based on its name, in the same way
> that the Python interpreter will happily take an absolute pathname for
> the main script. I'm sure there's a way to do it, but I don't know
> how. Obviously the import statement can't do it, but I've been poking
> around with __import__ and importlib without success. (Experiments are
> being done on Python 3.3; if a different version would make the job
> easier I'm happy to switch. This is just a curiosity tinkering.)
>
> Here's my current attempts (tracebacks chomped as they aren't very
> helpful here):
>
> >>> import "/x.py"
> SyntaxError: invalid syntax
> >>> importlib.import_module("/x.py")
> ImportError: No module named '/x'
> >>> importlib.import_module("/x")
> ImportError: No module named '/x'
>
> The best I can come up with is manually execing the file contents:
> >>> g={}
> >>> exec("def __main__():\n\tprint('Hello, world!')\n",g)
> >>> g["__main__"]()
> Hello, world!
>
> But that's not importing. Is there a way to do this as a module import?
>

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

This raises a few questions that should be addressed:

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 "</path/to/spam.py>", 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.

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


Thread

Re: Importing by file name Ian Kelly <ian.g.kelly@gmail.com> - 2013-11-24 01:00 -0700

csiph-web