473,387 Members | 1,441 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,387 software developers and data experts.

dynamic module loading via __import__, nonetype?

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 attempt to load a module at runtime
and then request an object of that type.

Essentially, my code works as follows:

I have a registry class which has a method to register a class. It
inserts an entry into a dictionary using a tuple of two strings (read
from attributes of the class) that points to the class. It also has a
method which will create an object of that class type which takes two
strings as an argument.

Every class i want to use this technique inherits from a class which
uses a custom metaclass. The custom metaclass' __init__ function
registers them with the registry class.

The registry class has another method which allows you to specify a path
to place your plugins in. In reality it merely appends this string to
sys.path. The registry "create" function has a little extra smarts in
that if it can't find the class already in the dictionary, it will
attempt to load the appropriate module. It does this by using __import__
and uses the two strings supplied with a dot in between as the module name.

If i manually import a module, this method works a treat, however if i
attempt to dynamically load a module at runtime the create method fails
with the following error:

TypeError: 'NoneType' object is not callable

I can see that the module is getting imported as i have set print
statements in the custom metaclass to let me know when it is registering
a new class. I can only conclude then that there is some difference
between using the import statement and the __import__ function.

I don't want to clutter the mailing list so i haven't attached any code,
but i certainly can supply it assuming the answer isn't something
obvious i've missed. Am i going about this completely the wrong way? Is
there already an established approach for an auto-registering,
dynamically loading plugin setup? Any help greatly appreciated.

I'm not subscribed to the list, so please cc me in any replies.

Thanks,

John Allman
Oct 16 '06 #1
0 1871

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Petri Savolainen | last post by:
I was trying to roll my own python code importer, but in the end, it seems that no matter what you try, it is always necessary to supply a REAL file object for python lower-level import machinery...
14
by: Alex Hunsley | last post by:
Does python provide a way to dynamically use modules and/or classes? I'm thinking in the vein of Java's Class.forName. As a pseudocode example, I'm looking for the following ability: ...
6
by: bry | last post by:
Hi, I'm trying to do a dynamic import of a file that has no problems with it, that is to say I can import it normally, my sys.path is set to the right folder etc. but my dynamic import code is not...
3
by: Marco Aschwanden | last post by:
Hi I have a script that looks for modules and tries to load them. After loading I attach the "server"-API function to it. module = __import__(module_name, globals(), locals(), ) module.server...
1
by: alain MONTMORY | last post by:
Hello everybody, I am a newbie to python so I hope I am at the right place to expose my problem..... :-http://www.python.org/doc/2.4.2/ext/pure-embedding.html 5.3 Pure Embedding I download the...
16
by: devicerandom | last post by:
Hi, I am currently using the Cmd module for a mixed cli+gui application. I am starting to refactor my code and it would be highly desirable if many commands could be built as simple plugins. ...
7
by: bambam | last post by:
import works in the main section of the module, but does not work as I hoped when run inside a function. That is, the modules import correctly, but are not visible to the enclosing (global)...
1
by: David Hirschfield | last post by:
I had a situation recently that required I manually load python bytecode from a .pyc file on disk. So, for the most part, I took code from imputil.py which loads the .pyc data via the marshal...
5
by: marcroy.olsen | last post by:
Hi list and python gurus :-) I'm playing with some mod_python and web development. And in me code I need to do som dynamic imports. Right now I just do a: exec 'import '+some_modulename ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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.