473,221 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

importing / loading a module / class dynamically

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
7 1930
.../.../../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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In...
2
by: Foehammer | last post by:
Hello, I'm trying to load an assembly dynamically using an app domain. This is a proof-of-concept for a larger project, so please excuse the lame class names. TestLib is the dll where all the...
5
by: Christoph Haas | last post by:
Dear coders... I'm working on an application that is supposed to support "plugins". The idea is to use the plugins as packages like this: Plugins/ __init__.py Plugin1.py Plugin2.py...
30
by: Franck PEREZ | last post by:
Hello, I'm developing a small XML marshaller and I'm facing an annoying issue. Here's some sample code: ########### My test application ############ class Foo(object): #The class I'd like to...
0
by: John Allman | last post by:
Hi all, I'm trying to create a setup which allows a program to request an object using strings and get an object of that type. It appears to be mostly working but i have difficulties if i...
5
by: Pete Marsh | last post by:
Wondering if anyone can recomend some sample code for dynamically loading the GD module. I have tried setting the extension dir in php.ini, and loading the GD module from there when apache is...
9
by: rbygscrsepda | last post by:
Hi, I'm a newbie at Python. :) Right now it's not letting me import * from any relative package name--i.e., a name that starts with a dot. For instance, none of the following work: from . import...
4
by: rshepard | last post by:
I'm stymied by what should be a simple Python task: accessing the value of a variable assigned in one module from within a second module. I wonder if someone here can help clarify my thinking. I've...
4
by: rkmr.em | last post by:
Hi I have a function data, that I need to import from a file data, in the directory data If I do this from python interactive shell (linux fedora core 8) from dir /home/mark it works fine: ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.