Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How do I import everything in a subdir?

Reply
Thread Tools

How do I import everything in a subdir?

 
 
Dfenestr8
Guest
Posts: n/a
 
      03-06-2005
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.rfind(".py")])
>>> for x in plugs:

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

 
Reply With Quote
 
 
 
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      03-06-2005
On Sun, 06 Mar 2005 15:20:42 +1000, Dfenestr8
<(E-Mail Removed)> 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



--
> ================================================== ============ <
> http://www.velocityreviews.com/forums/(E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
Reply With Quote
 
 
 
 
John Roth
Guest
Posts: n/a
 
      03-06-2005

"Dfenestr8" <(E-Mail Removed)> wrote in message
news(E-Mail Removed).. .
> 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.rfind(".py")])
>>>> for x in plugs:

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


 
Reply With Quote
 
YL
Guest
Posts: n/a
 
      03-07-2005
Try something like this:
for x in plugs:
cmd = "import %s" % x
exec (cmd)


"Dfenestr8" <(E-Mail Removed)> wrote in message
news(E-Mail Removed).. .
> 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.rfind(".py")])
> >>> for x in plugs:

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



 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      03-07-2005
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
 
Reply With Quote
 
Cyril BAZIN
Guest
Posts: n/a
 
      03-07-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
from calendar import* doesn't import everything Rotwang Python 3 04-25-2012 09:25 PM
Not Quite Everything for a Theory of Everything fitz VOIP 0 02-28-2010 04:42 PM
why not import everything? yawnmoth Java 42 02-21-2009 03:00 AM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments