Module Name Conflicts

Dan Sommers me at privacy.net
Thu Aug 18 19:39:19 EDT 2005


On 18 Aug 2005 16:06:46 -0700,
torched_smurf at yahoo.com wrote:

> I have a java program in a package called 'cmd'. This of course
> conflicts with the builtin python package of the same name. The thing
> is, I need to be able to import from both of these packages in the same
> script. I can import either one first, but any future attempt to import
> from cmd.* will look up the first cmd that was imported, so the second
> package is essentially eclipsed. I've tried fiddling with sys.path and
> sys.packageManager.searchPath, to no avail. To answer the obvious first
> suggestion, no I can't rename the java package to 'Cmd' or anything
> like that. Any ideas?

Assuming you can fiddle with sys.path at the right times, you can call
an imported module anything you want:

    fix_sys_path_to_find_java_cmd_first()
    import cmd as java_cmd
    fix_sys_path_to_find_python_cmd_first()
    import cmd as python_cmd

Obviously, then, 'cmd' does not reference either module; you'd have to
use java_cmd and python_cmd as appropriate.

HTH,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>



More information about the Python-list mailing list