By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,173 Members | 796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,173 IT Pros & Developers. It's quick & easy.

importing / loading a module / class dynamically

P: n/a
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)
>>s = 'test.py'
eval ('import ' + s)
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

Jan 5 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
.../.../../plugin/name1/name1.py
....
.../.../../plugin/namen/namen.py
I block at the beginning and tried this (test.py is a real file)
>>>s = 'test.py'
eval ('import ' + s)
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

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

P: n/a
hg
hg wrote:
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)
>>>s = 'test.py'
eval ('import ' + s)

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/pyt...ly/272081.html,
I need to use exec and not eval

hg

Jan 5 '07 #3

P: n/a
hg írta:
hg wrote:

>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)
>>>>s = 'test.py'
eval ('import ' + s)
>
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/pyt...ly/272081.html,
I need to use exec and not eval
Well, you can also use the 'imp' module. You should read this:

http://docs.python.org/lib/module-imp.html

Best,

Laszlo

Jan 5 '07 #4

P: n/a
hg
Laszlo Nagy wrote:
>
>.../.../../plugin/name1/name1.py
....
.../.../../plugin/namen/namen.py
I block at the beginning and tried this (test.py is a real file)
>>>>s = 'test.py'
eval ('import ' + s)
>
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

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

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

Jan 5 '07 #5

P: n/a
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.htm...00000000000000

Laszlo

Jan 5 '07 #6

P: n/a
hg
Laszlo Nagy wrote:
>
>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.htm...00000000000000

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
Jan 5 '07 #7

P: n/a
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 :
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)
>>>s = 'test.py'
eval ('import ' + s)

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
Jan 5 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.