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


Groups > comp.lang.python > #60356

Re: Importing by file name

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; 'interpreter': 0.05; 'modifying': 0.07; 'subject:file': 0.07; 'deprecated': 0.09; 'imported': 0.09; 'python': 0.11; '24,': 0.16; 'happily': 0.16; 'internally': 0.16; 'knock': 0.16; 'path.': 0.16; 'pathname': 0.16; 'syntax,': 0.16; 'sys.path': 0.16; 'undocumented': 0.16; 'for?': 0.16; 'wrote:': 0.18; 'module': 0.19; 'work,': 0.20; 'seems': 0.21; 'appears': 0.22; 'import': 0.22; 'script.': 0.24; 'script': 0.25; 'post': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'absolute': 0.30; 'relative': 0.30; 'message-id:@mail.gmail.com': 0.30; '(unless': 0.31; 'file': 0.32; 'another': 0.32; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'christian': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'further': 0.61; '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=H7HpN5tx90fDMDRqx+2OuwmRGr1rgpInWNiPHCLWLtc=; b=bsQrIJ7XGj+OOxlED8pyN2JJdPxDPPXgu3pcEUdBwd2/TRgsRV0R9a9qlDBhEwCyls jcIJx0zGXJay5IkWtKBfAMc2Z2SD7/98IqKZtxpm8naMspG+jLxsAaiGsIQvNizir0ao fBt6Am/UnXZQZB2+CEbCj7gZPssG8AWE+zLfv7tX7saFIeG1A1CdW9Ec7mJVW3ZyICvT jXp3iZXrn5Dx+YRY+wOGh88fjTra/hesUbW/v4ceN6Pt1HT760U9Y5UF2/5vHp961QkG voAYzX9eYhdI0UM8u+a+RFfLfg9r9szq5NI8TAqavUaTwxRWAbXOBqJL6W/ITzBkp5oo OElQ==
X-Received by 10.66.218.198 with SMTP id pi6mr21724881pac.107.1385286677458; Sun, 24 Nov 2013 01:51:17 -0800 (PST)
MIME-Version 1.0
In-Reply-To <l6sg89$bns$1@dont-email.me>
References <mailman.3114.1385264464.18130.python-list@python.org> <l6sg89$bns$1@dont-email.me>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 24 Nov 2013 02:50:37 -0700
Subject Re: Importing by file name
To Python <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 <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.3120.1385286687.18130.python-list@python.org> (permalink)
Lines 21
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385286687 news.xs4all.nl 15957 [2001:888:2000:d::a6]:36081
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!news.franciliens.net!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:60356

Show key headers only | View raw


On Sun, Nov 24, 2013 at 2:18 AM, Christian Gollwitzer <auriocus@gmx.de> wrote:
> Am 24.11.13 04:41, schrieb Chris Angelico:
>
>> 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.
>
>
> Is it imp.load_source() that you are looking for?

That appears to work, but I note that it's undocumented and seems to
be used internally only by the deprecated load_module function.  I
expect it will likely be removed in Python 3.5.

> Modifying sys.path is only necessary because the file could further import
> modules from the same path.

This won't work if the imported file is part of a package and attempts
to import another module in the same package (unless it uses the old
relative import syntax, which was removed in Python 3).

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


Thread

Importing by file name Chris Angelico <rosuav@gmail.com> - 2013-11-24 14:41 +1100
  Re: Importing by file name Christian Gollwitzer <auriocus@gmx.de> - 2013-11-24 10:18 +0100
    Re: Importing by file name Ian Kelly <ian.g.kelly@gmail.com> - 2013-11-24 02:50 -0700
    Re: Importing by file name Chris Angelico <rosuav@gmail.com> - 2013-11-24 22:05 +1100
    Re: Importing by file name Ian Kelly <ian.g.kelly@gmail.com> - 2013-11-24 04:37 -0700

csiph-web