Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > importing / loading a module / class dynamically

Reply
Thread Tools

importing / loading a module / class dynamically

 
 
hg
Guest
Posts: n/a
 
      01-05-2007
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

 
Reply With Quote
 
 
 
 
hg
Guest
Posts: n/a
 
      01-05-2007
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

 
Reply With Quote
 
 
 
 
hg
Guest
Posts: n/a
 
      01-05-2007
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



 
Reply With Quote
 
hg
Guest
Posts: n/a
 
      01-05-2007
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


 
Reply With Quote
 
Laszlo Nagy
Guest
Posts: n/a
 
      01-05-2007

> .../.../../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
>


 
Reply With Quote
 
Laszlo Nagy
Guest
Posts: n/a
 
      01-05-2007
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

 
Reply With Quote
 
Laszlo Nagy
Guest
Posts: n/a
 
      01-05-2007

> 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

 
Reply With Quote
 
laurent rahuel
Guest
Posts: n/a
 
      01-05-2007
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
>

 
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
dynamically importing a module and function rkmr.em@gmail.com Python 4 04-24-2008 04:19 AM
Importing Module To Use Variables In A Second Module rshepard@nospam.appl-ecosys.com Python 4 09-28-2007 01:17 PM
doubt with importing module, given module name Pradnyesh Sawant Python 0 04-09-2007 05:22 PM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
Using class without importing module Thomas Aanensen Python 1 02-10-2004 10:50 PM



Advertisments