importing two modules with the same name

El Pitonero pitonero at gmail.com
Sat Mar 19 17:59:29 EST 2005


Francisco Borges wrote:
> There are 2 "foo" named modules, 'std foo' and 'my foo'. I want to be
> able to import 'my foo' and then from within my foo, import 'std
> foo'. Anyone can help??

In other words, you would like to make a "patch" on third-party code.
There are many ways to do it. Here is just one possible approach.

#--------- A.py: third-party module
x = 3
def f():
    return 'A.f(): x=%d' % x
#--------- B.py: your modifications to the module
def f():
    return 'B.f(): x=%d' % x
#--------- Main.py: your program
import imp
# load the third party module into sys.modules
imp.load_source('A', '', open('C:\\A.py'))
# load and execute your changes
imp.load_source('A', '', open('C:\\B.py'))
# import now from memory (sys.modules)
import A
print A.f()




More information about the Python-list mailing list