How do I import everything in a subdir?

Discussion in 'Python' started by Dfenestr8, Mar 6, 2005.

  1. Dfenestr8

    Dfenestr8 Guest

    Hi.

    I have a program which I want a plugin directory for. I figured the way to
    go about that would be to just add a plugin/ dir to sys.path, and import
    everything in it. Then my program can just execute the main() method of
    each imported plugin.

    Is that a good way to go about it?

    If so, how do I import everything in the plugins dir? The method raises an
    error as you can see.

    >>> import sys
    >>> import os
    >>> sys.path.append("plugins")
    >>> ls = os.popen("ls plugins").readlines()
    >>> for x in ls:

    .... plugs.append(x[0:x.rfind(".py")])
    >>> for x in plugs:

    .... import x
    ....
    Traceback (most recent call last):
    File "<stdin>", line 2, in ?
    ImportError: No module named x
     
    Dfenestr8, Mar 6, 2005
    #1
    1. Advertising

  2. On Sun, 06 Mar 2005 15:20:42 +1000, Dfenestr8
    <> declaimed the following in
    comp.lang.python:


    > >>> sys.path.append("plugins")
    > >>> ls = os.popen("ls plugins").readlines()


    There are easier, Python-native ways to get the contents of the
    directories.

    > >>> for x in plugs:

    > ... import x
    > ...
    > Traceback (most recent call last):
    > File "<stdin>", line 2, in ?
    > ImportError: No module named x


    No surprise there... The import /statement/ does not treat its
    argument as a variable to do a look-up against -- if it did, you'd be
    writing stuff like:

    x = "os"
    import x

    or

    import "sys"

    The argument to import is treated /as/ a literal, even though
    lacking quotes.

    Check the manuals on

    __import__() {built-in function}

    or the imp module



    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Mar 6, 2005
    #2
    1. Advertising

  3. Dfenestr8

    John Roth Guest

    "Dfenestr8" <> wrote in message
    news:p...
    > Hi.
    >
    > I have a program which I want a plugin directory for. I figured the way to
    > go about that would be to just add a plugin/ dir to sys.path, and import
    > everything in it. Then my program can just execute the main() method of
    > each imported plugin.
    >
    > Is that a good way to go about it?
    >
    > If so, how do I import everything in the plugins dir? The method raises an
    > error as you can see.


    Read the directory and then use the __import__() method on each
    entry that ends in .py, .pyc or .pyo. Filter for duplicates first or you
    may be executing a single plugin more than once.

    Don't bother with sys.path unless you want your plugins to be
    able to import from that directory as well.

    John Roth


    >
    >>>> import sys
    >>>> import os
    >>>> sys.path.append("plugins")
    >>>> ls = os.popen("ls plugins").readlines()
    >>>> for x in ls:

    > ... plugs.append(x[0:x.rfind(".py")])
    >>>> for x in plugs:

    > ... import x
    > ...
    > Traceback (most recent call last):
    > File "<stdin>", line 2, in ?
    > ImportError: No module named x
    >
     
    John Roth, Mar 6, 2005
    #3
  4. Dfenestr8

    YL Guest

    Try something like this:
    for x in plugs:
    cmd = "import %s" % x
    exec (cmd)


    "Dfenestr8" <> wrote in message
    news:p...
    > Hi.
    >
    > I have a program which I want a plugin directory for. I figured the way to
    > go about that would be to just add a plugin/ dir to sys.path, and import
    > everything in it. Then my program can just execute the main() method of
    > each imported plugin.
    >
    > Is that a good way to go about it?
    >
    > If so, how do I import everything in the plugins dir? The method raises an
    > error as you can see.
    >
    > >>> import sys
    > >>> import os
    > >>> sys.path.append("plugins")
    > >>> ls = os.popen("ls plugins").readlines()
    > >>> for x in ls:

    > ... plugs.append(x[0:x.rfind(".py")])
    > >>> for x in plugs:

    > ... import x
    > ...
    > Traceback (most recent call last):
    > File "<stdin>", line 2, in ?
    > ImportError: No module named x
    >
     
    YL, Mar 7, 2005
    #4
  5. YL wrote:
    > Try something like this:
    > for x in plugs:
    > cmd = "import %s" % x
    > exec (cmd)


    For the sake of others who might have missed the rest of this thread,
    I'll point out that this is definitely not the way to go. No need to
    use exec when the builtin __import__ function is already defined for
    exactly this sort of use.

    STeVe
     
    Steven Bethard, Mar 7, 2005
    #5
  6. Dfenestr8

    Cyril BAZIN Guest

    Hello,

    If you want to look for the files "*.py" in a directory, don't use
    shell command!!!
    You have many ways to access the content of a directory in Python.

    For exemple, you can use the glob module:

    >>> import glob
    >>> glob.glob('./[0-9].*')

    ['./1.gif', './2.txt']
    >>> glob.glob('*.gif')

    ['1.gif', 'card.gif']
    >>> glob.glob('?.gif')

    ['1.gif']

    You might look at this page "http://docs.python.org/lib/module-glob.html".

    Cyril
     
    Cyril BAZIN, Mar 7, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michel Gallant
    Replies:
    0
    Views:
    424
    Michel Gallant
    Oct 14, 2005
  2. news-server

    View a list of files in a SubDir

    news-server, Jul 11, 2003, in forum: HTML
    Replies:
    1
    Views:
    434
    David Dorward
    Jul 11, 2003
  3. Peter Van Hove
    Replies:
    3
    Views:
    576
    Peter Van Hove
    Apr 22, 2005
  4. =?Utf-8?B?bWF0bHlu?=

    creating a file within a subdir in DOS batch file

    =?Utf-8?B?bWF0bHlu?=, Sep 20, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    671
    =?Utf-8?B?bWF0bHlu?=
    Sep 20, 2007
  5. Rotwang
    Replies:
    3
    Views:
    308
    Rotwang
    Apr 25, 2012
Loading...

Share This Page