FAQ
Hello,

I was wondering if there was something like Perl's "require" that allows
you to import a file whose name is specified at run-time. So far I've only
seen imports of modules that are put in the standard module include path.

I'm interested in three seperate problems:

1) being able to import a file that isn't in the standard module include path

2) being able to import a file whose name is specified in a python string

3) being able to reload the file if it changes on disk

I'm pretty sure that the Pylons web framework does all this, and I'm also
pretty sure that there's a simple solution, I am just not aware of what
it is.
--
Obama Nation | My emails do not have attachments; it's a digital signature
that your mail program doesn't understand. | http://www.subspacefield.org/~travis/
If you are a spammer, please email john at subspacefield.org to get blacklisted.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 850 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20090905/7267694b/attachment.pgp>

Search Discussions

  • Chris Rebert at Sep 5, 2009 at 10:25 pm

    On Sat, Sep 5, 2009 at 3:23 PM, wrote:
    Hello,

    I was wondering if there was something like Perl's "require" that allows
    you to import a file whose name is specified at run-time. ?So far I've only
    seen imports of modules that are put in the standard module include path.
    You'll need to use the `imp` module:
    http://docs.python.org/library/imp.html#module-imp

    Cheers,
    Chris
  • Stephen Fairchild at Sep 5, 2009 at 11:23 pm

    travis+ml-python at subspacefield.org wrote:

    I'm interested in three seperate problems:

    1) being able to import a file that isn't in the standard module include
    path
    sys.path.insert(0, "/path/to/module"
    module = __import__("module")
    del sys.path[0]

    Ideally this goes into a function, possibly with os.path.split to break
    apart absolute paths.
    2) being able to import a file whose name is specified in a python string
    sys = __import__("sys")
    3) being able to reload the file if it changes on disk
    sys = reload(sys) # sys must have been imported already

    Maybe use in conjunction with python-inotify and or Gamin for change
    detection.
    --
    Stephen Fairchild

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 5, '09 at 10:23p
activeSep 5, '09 at 11:23p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase