Python:导入异常
是否可以从包中导入除一个模块之外的所有内容?
我需要在类中使用的特定库中的许多模块,但看起来它对我需要的模块之一使用了相同的模块名称。
我需要使用集合运算和交集,但是当我从类中导入该库时,它会因此给我一个错误。
我不想单独导入它或将名称放在每个方法前面,因为我经常使用它。
除了像 set
这样的特定方法之外,Python 有没有办法导入所有内容? 或者稍后再次导入 set
部分?
Is it possible to import everything except one module from a package?
I need a lot of modules from a particular library that I use in my class, but it looks like it used the same module name for one of the modules that I need.
I need to use set operation and intersection, but when I import that library from my class, it gives me an error because of that.
I didn't want to import it separately or put the name in front of every methods since I'm using it a lot.
Is there a way for the python to import everything except for a particular method like set
?
Or maybe import the set
part again later?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,
from ... import * 没有任何术语,除了 blah、bleh、bluh
。您可以编写自己的导入函数来支持它,或者执行以下操作:这将停止隐藏内置
set
,以便您可以再次使用它。然后,如果您需要xyz.set
函数,您可以执行以下操作:注意:
from ... import *
通常不是一个好的做法,并且您应该确保您以这种方式使用的模块支持它——如果它们没有明确表示它们是设计用于这种方式的,那么您不应该这样做(除非您喜欢稍后调试奇怪的问题;)。No, there is no terminology to
from ... import * except blah, bleh, bluh
. You can either write your own import function to support it, or do something like:which will stop shadowing the built-in
set
so you can use it again. Then if you need thexyz.set
function you can do:Note:
from ... import *
is not usually good practice, and you should make sure the modules you are using this way support it -- if they don't explicity say they were designed to be used this way, then you shouldn't (unless you enjoy debugging weird problems later ;).我想你想要的是:
将从
thing
导入a, b, c, d, e, f
。AFAIK,除了 a、b、c 之外,没有办法进行
from thing import
这就是为什么
存在的首要原因。
I suppose what you want would be:
which will import
a, b, c, d, e, f
fromthing
.AFAIK, there is no way to do an
from thing import all but a, b, c
thats why
exists in the first place.
我不确定我完全理解发生了什么(给出实际的模块名称可能会有所帮助)。但通常认为
from ... import *
是不好的做法,因为这样就不清楚特定的东西来自哪里。相反,请执行from ... import thingA, thingB, thingC
。您还可以执行
import ... as Shortname
操作,然后将方法引用为shortname.whatever
(其中shortname
显然可能是非常重要的东西)短的)。I'm not sure I entirely understand what's going on (giving actual module names might help). But it's generally considered bad practice to do
from ... import *
at all, because then it's not obvious where particular things are coming from. Instead, dofrom ... import thingA, thingB, thingC
.You could also do
import ... as shortname
, and then refer to the methods asshortname.whatever
(whereshortname
could obviously be something very short).