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


Groups > comp.lang.python > #60348 > unrolled thread

Importing by file name

Started byChris Angelico <rosuav@gmail.com>
First post2013-11-24 14:41 +1100
Last post2013-11-24 04:37 -0700
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#60348 — Importing by file name

FromChris Angelico <rosuav@gmail.com>
Date2013-11-24 14:41 +1100
SubjectImporting by file name
Message-ID<mailman.3114.1385264464.18130.python-list@python.org>
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?

ChrisA

[toc] | [next] | [standalone]


#60355

FromChristian Gollwitzer <auriocus@gmx.de>
Date2013-11-24 10:18 +0100
Message-ID<l6sg89$bns$1@dont-email.me>
In reply to#60348
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?

I'm using a similar thing for a plugin system, the plugins are stored 
with a fixed filename in a folder structure. The code looks like 
this(stripped down):

import imp

# folder is the path to the file, under which pluginmain.py
# contains the code

namespace = 'plugin{0}'.format(plugincount)
plugincount = plugincount + 1

# try to import the file and exec the init function
# if anything fails, set success=False, errorpos and errorinfo

# add path to the plugin into import path
oldsyspath=sys.path[:]
sys.path.insert(0, folder)

# load module
module = imp.load_source(namespace,os.path.join(folder,'pluginmain.py'))

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

	Christian

[toc] | [prev] | [next] | [standalone]


#60356

FromIan Kelly <ian.g.kelly@gmail.com>
Date2013-11-24 02:50 -0700
Message-ID<mailman.3120.1385286687.18130.python-list@python.org>
In reply to#60355
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).

[toc] | [prev] | [next] | [standalone]


#60361

FromChris Angelico <rosuav@gmail.com>
Date2013-11-24 22:05 +1100
Message-ID<mailman.3125.1385291127.18130.python-list@python.org>
In reply to#60355
On Sun, Nov 24, 2013 at 8:50 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> 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.

Undocumented... that explains why I didn't know about it! But that
does appear to be what I'm looking for, so is there some equivalent
planned as a replacement?

ChrisA

[toc] | [prev] | [next] | [standalone]


#60362

FromIan Kelly <ian.g.kelly@gmail.com>
Date2013-11-24 04:37 -0700
Message-ID<mailman.3126.1385293064.18130.python-list@python.org>
In reply to#60355
On Sun, Nov 24, 2013 at 4:05 AM, Chris Angelico <rosuav@gmail.com> wrote:
> Undocumented... that explains why I didn't know about it! But that
> does appear to be what I'm looking for, so is there some equivalent
> planned as a replacement?

Hmm, playing around with importlib a bit, this seems to work:

from importlib import find_loader
loader = find_loader('spam', ['/path/to'])
if loader is not None:
    module = loader.load_module()

The path passed to find_loader is searched instead of sys.path.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web