By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,002 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

importing two modules with the same name

P: n/a
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
`\<,
_____(*)/(*)_____

Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Francisco Borges <bo****@let.rug.nl> writes:
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #2

P: n/a
Francisco Borges wrote:
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?

--
Website: www DOT jarmania FULLSTOP com
Jul 18 '05 #3

P: n/a
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()

Jul 18 '05 #4

P: n/a
Tim Jarman wrote:
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.

Jul 18 '05 #5

P: n/a
El Pitonero wrote:
#--------- 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

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.