Module Name Conflicts

Robert Kern rkern at ucsd.edu
Fri Aug 19 01:46:42 CEST 2005


Dan Sommers wrote:

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

That doesn't work. The first module is recorded as 'cmd' in sys.modules 
and gets reused on the second import.

[~]$ mkdir foo1
[~]$ mkdir foo2
[~]$ touch foo1/blah.py
[~]$ touch foo2/blah.py
[~]$ python
Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> import sys
 >>> sys.path.insert(0,'foo1')
 >>> import blah as blah1
 >>> sys.path.insert(0,'foo2')
 >>> import blah as blah2
 >>> sys.modules['blah']
<module 'blah' from 'foo1/blah.py'>
 >>> blah2.__file__
'foo1/blah.py'
 >>>

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list