Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Module Name Conflicts

Reply
Thread Tools

Module Name Conflicts

 
 
torched_smurf@yahoo.com
Guest
Posts: n/a
 
      08-18-2005
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?

-Smurf

 
Reply With Quote
 
 
 
 
ncf
Guest
Posts: n/a
 
      08-18-2005
Maybe what you're looking for is __import__()?

>>> help(__import__)

Help on built-in function __import__ in module __builtin__:

__import__(...)
__import__(name, globals, locals, fromlist) -> module

Import a module. The globals are only used to determine the
context;
they are not modified. The locals are currently unused. The
fromlist
should be a list of names to emulate ``from name import ...'', or
an
empty list to emulate ``import name''.
When importing a module from a package, note that __import__('A.B',
....)
returns package A when fromlist is empty, but its submodule B when
fromlist is not empty.

 
Reply With Quote
 
 
 
 
Dan Sommers
Guest
Posts: n/a
 
      08-18-2005
On 18 Aug 2005 16:06:46 -0700,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/>
 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      08-18-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
(E-Mail Removed)

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

 
Reply With Quote
 
torched_smurf@yahoo.com
Guest
Posts: n/a
 
      08-18-2005
Robert Kern wrote:
> 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.


Exactly. And clearing sys.modules doesn't fix the problem. Once it's
imported something from the first cmd package, it can no longer find
anything in another cmd package; it will always look for it in that
first package.

-Smurf

 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      08-19-2005
(E-Mail Removed) 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?


Why not copy cmd.py into your package under a different name?


--
Robert Kern
(E-Mail Removed)

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

 
Reply With Quote
 
torched_smurf@yahoo.com
Guest
Posts: n/a
 
      08-19-2005
ncf wrote:
> Maybe what you're looking for is __import__()?
>
> >>> help(__import__)

> Help on built-in function __import__ in module __builtin__:
>
> __import__(...)
> __import__(name, globals, locals, fromlist) -> module
>
> Import a module. The globals are only used to determine the
> context;
> they are not modified. The locals are currently unused. The
> fromlist
> should be a list of names to emulate ``from name import ...'', or
> an
> empty list to emulate ``import name''.
> When importing a module from a package, note that __import__('A.B',
> ...)
> returns package A when fromlist is empty, but its submodule B when
> fromlist is not empty.


Using this doesn't appear to work any better than regular old import.

-Smurf

 
Reply With Quote
 
torched_smurf@yahoo.com
Guest
Posts: n/a
 
      08-19-2005
Robert Kern wrote:
> Why not copy cmd.py into your package under a different name?
>


It offends my sense of modularity. For the record, I'm trying to use
pdb, the debugger, which in turn uses cmd. So it would be a matter of
taking pdb.py and hacking it to import a renamed version of cmd... kind
of messy and not a very good longterm solution. That's not to say I
won't resort to it if no better options are forthcoming.

-Smurf

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      08-19-2005
(E-Mail Removed) wrote:
> Robert Kern wrote:
>>That doesn't work. The first module is recorded as 'cmd' in sys.modules
>>and gets reused on the second import.

>
> Exactly. And clearing sys.modules doesn't fix the problem. Once it's
> imported something from the first cmd package, it can no longer find
> anything in another cmd package; it will always look for it in that
> first package.


That part isn't correct. Removing the entry from sys.modules should
(and has, for me, in the past) worked fine to let a second import reload
a module, or find a new module after sys.path has been tweaked. Try it
again.

-Peter
 
Reply With Quote
 
ncf
Guest
Posts: n/a
 
      08-19-2005
I'm honestly not too sure how __import__ works, but I know you can
provide a full path to it. Oh well, that was my best guess. I wish I
could've been of more help. -Wes

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
resolving module name conflicts. Gelonida N Python 0 11-11-2011 08:27 PM
Module Conflicts Jose Python 7 04-11-2008 04:05 AM
Namespace conflicts with page class - name resolution difference in compiler Shadow Lynx ASP .Net 1 02-04-2006 04:17 PM
Handling import conflicts when module has the same name as a library module that it needs to import? plb Python 0 02-08-2005 01:08 PM
Handling import conflicts when module has the same name as a library module that it needs to import? plb Python 0 02-08-2005 01:01 PM



Advertisments