importing / loading a module / class dynamically

H

hg

Hi,

I have the following problem.

I find in a directory hierarchy some files following a certain sets of
rules:

..../.../../plugin/name1/name1.py
.....
..../.../../plugin/namen/namen.py

each file will in turn have a class with the same name as the filename
(minus .py)


I fetch those names in a list of string and want to import the files /
instantiate the classes.


I block at the beginning and tried this (test.py is a real file)
and get

Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
eval ('import ' + s)
File "<string>", line 1
import test.py

Any clue ?

Thanks

hg
 
H

hg

hg said:
Hi,

I have the following problem.

I find in a directory hierarchy some files following a certain sets of
rules:

.../.../../plugin/name1/name1.py
....
.../.../../plugin/namen/namen.py

each file will in turn have a class with the same name as the filename
(minus .py)


I fetch those names in a list of string and want to import the files /
instantiate the classes.


I block at the beginning and tried this (test.py is a real file)

and get

Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
eval ('import ' + s)
File "<string>", line 1
import test.py

Any clue ?

Thanks

hg


OK, from http://mail.python.org/pipermail/python-list/2004-July/272081.html,
I need to use exec and not eval

hg
 
H

hg

Laszlo said:
import test.py # This is invalid
import test # This MAY be valid
import name1.name1 # Most probably this is what you want if you have the
aforementioned directory stucture
from name1 import name1 # Or this?

You must also:

1. Have the 'plugin' dir in your sys.path
2. Have at least an empty plugin/name1/__init__.py file

Another alternative is to have plugins/__init__.py and do something like:

from plugins.name1 import name1

You should not overcomplicate things anyway. If you do not need these
name1...namen directories for sure, then just drop them.

Hint: try this (untested)

import os
fnames = os.listdir('plugins')
for fname in fnames:
if os.path.isdir(fname):
root,ext = os.path.splitext(fname)
cmd = "from plugins.%s import %s" % (root,root)
print "I should eval this:",cmd

Best,

Laszlo


Thanks,

What I am doing is adding plugin support to PyCrust ... so I'm looking for a
mechanism where anyone can develop a plugin and have it loaded by pycrust.

the .py was a typo


why the "...Have at least an empty plugin/name1/__init__.py file..." ?

Thanks,

hg
 
H

hg

Laszlo said:
When you do

import plugins.name1.name1

then "plugins" and "plugins/name1" should be a package, not a module. A
package is a special directory that contains package initialization code
in a __init__.py file. If you do not have the file, then the "plugins"
directory will be only a directory, and it cannot be imported.

For details, see:

http://docs.python.org/tut/node8.html#SECTION008400000000000000000

Laszlo


Many thanks Laszlo, it looks like I got it to work ... result will be on
www.snakecard.com/PY for those interested ... in the next few days

hg
 
L

Laszlo Nagy

.../.../../plugin/name1/name1.py
....
.../.../../plugin/namen/namen.py


I block at the beginning and tried this (test.py is a real file)
import test.py # This is invalid
import test # This MAY be valid
import name1.name1 # Most probably this is what you want if you have the
aforementioned directory stucture
from name1 import name1 # Or this?

You must also:

1. Have the 'plugin' dir in your sys.path
2. Have at least an empty plugin/name1/__init__.py file

Another alternative is to have plugins/__init__.py and do something like:

from plugins.name1 import name1

You should not overcomplicate things anyway. If you do not need these
name1...namen directories for sure, then just drop them.

Hint: try this (untested)

import os
fnames = os.listdir('plugins')
for fname in fnames:
if os.path.isdir(fname):
root,ext = os.path.splitext(fname)
cmd = "from plugins.%s import %s" % (root,root)
print "I should eval this:",cmd

Best,

Laszlo
 
L

Laszlo Nagy

Thanks,

What I am doing is adding plugin support to PyCrust ... so I'm looking for a
mechanism where anyone can develop a plugin and have it loaded by pycrust.

the .py was a typo


why the "...Have at least an empty plugin/name1/__init__.py file..." ?
When you do

import plugins.name1.name1

then "plugins" and "plugins/name1" should be a package, not a module. A
package is a special directory that contains package initialization code
in a __init__.py file. If you do not have the file, then the "plugins"
directory will be only a directory, and it cannot be imported.

For details, see:

http://docs.python.org/tut/node8.html#SECTION008400000000000000000

Laszlo
 
L

laurent rahuel

Hi,

Using exec or eval ISN'T what should be done ever. When you have
troubles importing you should :

- Add some repository to your sys.path
and/or
- Use the buildin import method
and/or
- Use Mr Eby's Importing module (http://python.org/pypi/Importing)

Regards,

Laurent

hg a écrit :
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top