On Thu, Aug 09, 2001 at 03:11:11PM -0700, David Dunkle wrote:
| I would like to be able to import a Python module but
| not from a file. For example, I may want to store Python source
| in an RDBMS and import it without the file system being involved.
|
| From Java I would do this with by writing a classloader as part of
| my application. How can I do this with Jython?
|
| I realize that I could probably subclass PackageManager and roll
| my own. Is there a better way? Has anyone already solved this
| problem?
Define a function that will implement the import operation.
-----------------------------
Python 2.1 (#1, Apr 17 2001, 09:45:01)
[GCC 2.95.3-2 (cygwin special)] on cygwin_nt-4.01
Type "copyright", "credits" or "license" for more information.
>>> import __builtin__
>>> print __builtin__.__import__.__doc__
__import__(name, globals, locals, fromlist) -> module
Import a module. The globals are only used to determine the context;
they are not modified. The locals are currently unused. The fromlist
should be a list of names to emulate ``from name import ...'', or an
empty list to emulate ``import name''.
When importing a module from a package, note that __import__('A.B',
...)
returns package A when fromlist is empty, but its submodule B when
fromlist is not empty.
>>>
-----------------------------
Suppose I wanted to add printing out the package/module name to the
import :
# get access to the builtin stuff
import __builtin__
# save a reference to the original implementation
orig_import = __builtin__.__import__
# define how I want to import
def my_cust_import( name , globals , locals , fromlist ) :
print "Importing '%s' now" % name
return orig_import( name , globals , locals , fromlist )
# make python use my import instead
__builtin__.__import__ = my_cust_import
HTH,
-D
|