importing two modules with the same name

F

Francisco Borges

Hello,

This is not stricly necessary but it would be nice if I could get it
done. Here is what I want to do:

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??

---------

Before you start calling me stupid... the reason I would like that is
that:

I have a module that produces shell completion code from optparse
objects for zsh, tcsh and (sort of) bash. But to use it, I have to
directly modify the python code. Which I don't want that since I want to
be able to generate completion automatically for code which is not mine
in an easy manner.

So a shell script sets the PYTHONPATH in a way that "my optparse" is
loaded, but to build the option parser I want to then import the
original optparse module from my optparse.py file.

I could simply copy optparse's code and hack it or I could simply import
it and overload some methods, which is what I think would be a cleaner
solution.

Cheers,
--
Francisco.
Groningen, Nederlands.
__o
`\<,
_____(*)/(*)_____
 
M

Mike Meyer

Francisco Borges said:
I could simply copy optparse's code and hack it or I could simply import
it and overload some methods, which is what I think would be a cleaner
solution.

So why aren't you willing to give your optparse module a different name?

<mike
 
T

Tim Jarman

Francisco said:
Hello,

This is not stricly necessary but it would be nice if I could get it
done. Here is what I want to do:

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??

If I had to do this, I'd use packages:

import foo # the standard module - presumably already on sys.path
import my.foo # my module which lives in its own little world

Remember to qualify names correctly, and/or do something like:

import foo as std_foo

But if your foo is under your control, why not do everyone a favour and call
it something else?
 
E

El Pitonero

Francisco said:
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()
 
E

El Pitonero

Tim said:
But if your foo is under your control, why not do everyone a favour and call
it something else?

His case is a canonical example of a patch. Often you'd like to choose
the "patch" approach because:

(1) the third-party may eventually incorporate the changes themselves,
hence you may want to minimize changes to the name of the module in
your code, so one day in the future you may simply remove the patch, or

(2) you are testing out several ideas, at any point you may want to
change to a different patch, or simply roll back to the original
module, or

(3) your product is shipped to different clients, each one of them
requires a different twist of the shared module, or

(4) your program and your data files are versioned, and your program
structure needs cumulative patches in order to be able to work with all
previous data file versions.

These types of needs are rather common.
 
F

Francisco Borges

El said:
#--------- 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

Thanks a bunch for the hint. The imp module did take care of the job,
but just to mention load_source seems to be deprecated, so I used

fp, mpath, desc = imp.find_module('optparse',a)
s_opt = imp.load_module('std_optparse', fp, mpath, desc)

again, thanks for the help!

Cheers,
Francisco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top