Re: Module Name Conflicts

2005-08-18 Thread torched_smurf
ncf wrote:
> Maybe what you're looking for is __import__()?

Okay, actually this does work, but only in one direction. That is, I
can import the python package first, and then the java package, but not
the other way around.

--
Importing the python cmd first:

$~> jython
Jython 2.2a1 on java1.4.2_08 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> __import__('cmd')

 python package
>>> import sys
>>> sys.path.insert(0, java_classpath)
>>> sys.modules.clear()
>>> __import__('cmd')

 java package

--
Importing the java cmd first:

$~> jython
Jython 2.2a1 on java1.4.2_08 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.insert(0, java_classpath)
>>> __import__('cmd')

 java package
>>> sys.path = sys.path[1:]
>>> sys.modules.clear()
>>> __import__('cmd')

 java package, again
--

Very odd.

-Smurf

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Module Name Conflicts

2005-08-18 Thread torched_smurf
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Module Name Conflicts

2005-08-18 Thread torched_smurf
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Module Name Conflicts

2005-08-18 Thread torched_smurf
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Module Name Conflicts

2005-08-18 Thread torched_smurf
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

-- 
http://mail.python.org/mailman/listinfo/python-list