How do I import everything in a subdir?

D

Dfenestr8

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
 
D

Dennis Lee Bieber

There are easier, Python-native ways to get the contents of the
directories.
... 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



--
 
J

John Roth

Dfenestr8 said:
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
 
Y

YL

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


Dfenestr8 said:
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
 
S

Steven Bethard

YL said:
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
 
C

Cyril BAZIN

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top