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


Groups > comp.lang.python > #60356

Re: Importing by file name

References <mailman.3114.1385264464.18130.python-list@python.org> <l6sg89$bns$1@dont-email.me>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2013-11-24 02:50 -0700
Subject Re: Importing by file name
Newsgroups comp.lang.python
Message-ID <mailman.3120.1385286687.18130.python-list@python.org> (permalink)

Show all headers | 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