473,394 Members | 1,721 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,394 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 1944
.../.../../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: ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.