模块名称冲突 [英] Module Name Conflicts

查看:54
本文介绍了模块名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为''cmd''的程序包中有一个java程序。这当然是

与同名的内置python包冲突。这件事是

,我需要能够在同一个

脚本中从这两个包中导入。我可以先导入任何一个,但是将来任何从cmd。*导入

的尝试都将查找导入的第一个cmd,因此第二个

包基本上是黯然失色的。我试过摆弄sys.path和

sys.packageManager.searchPath,但无济于事。要回答明显的第一个

建议,不,我不能将java包重命名为''Cmd''或任何类似的东西

。有什么想法吗?


-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

推荐答案

也许你在找什么是__import __()?
Maybe what you''re looking for is __import__()?
help(__ import __)
help(__import__)



模块中的内置函数__import__的帮助__builtin__:


__import __(...)

__import __(name,globals, locals,fromlist) - >模块


导入模块。全局变量仅用于确定

上下文;

它们未被修改。当地人目前尚未使用。

fromlist

应该是一个名称列表来模拟``from name import ...'''',或

an

空列表来模拟``import name''''。

从包中导入模块时,请注意__import __('''A.B'',

....)
当fromlist为空时,
返回包A,但是当

fromlist时它的子模块B不为空。


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.


2005年8月18日16:06:46 -0700,
到*********** @ yahoo.com 写道:
On 18 Aug 2005 16:06:46 -0700,
to***********@yahoo.com wrote:
我在一个名为''的软件包中有一个java程序CMD'。这当然与同名的内置python包冲突。
是的,我需要能够在同一个
脚本中从这两个包中导入。我可以先导入任何一个,但是将来从cmd。*导入的任何尝试都将查找导入的第一个cmd,因此第二个
包基本上是重叠的。我试过摆弄sys.path和
sys.packageManager.searchPath,但没有用。为了回答明显的第一个建议,不,我不能将java包重命名为''Cmd''或类似的任何东西。任何想法?
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?




假设您可以在合适的时间摆弄sys.path,您可以随意调用

导入的模块:


fix_sys_path_to_find_java_cmd_first()

导入cmd为java_cmd

fix_sys_path_to_find_python_cmd_first()

导入cmd为python_cmd


显然,''cmd''没有引用任何一个模块;你必须使用java_cmd和python_cmd来支付



HTH,

Dan


-

Dan Sommers

< http://www.tombstonezero.net/dan/>



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


Dan Sommers写道:
Dan Sommers wrote:
假设您可以在合适的时间摆弄sys.path,您可以随意调用导入的模块:

fix_sys_path_to_find_java_cmd_first()
将cmd导入为java_cmd
fix_sys_path_to_find_python_cmd_first()
将cmd导入为python_cmd

显然,''cmd' '没有引用任何一个模块;你必须在适当的时候使用java_cmd和python_cmd。
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.




那是行不通的。第一个模块在sys.modules

中记录为''cmd'',并在第二个导入时重复使用。


[〜]



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

[~]


这篇关于模块名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆