Python:导入异常

发布于 2024-12-19 15:15:55 字数 257 浏览 0 评论 0原文

是否可以从包中导入除一个模块之外的所有内容?

我需要在类中使用的特定库中的许多模块,但看起来它对我需要的模块之一使用了相同的模块名称。

我需要使用集合运算和交集,但是当我从类中导入该库时,它会因此给我一个错误。

我不想单独导入它或将名称放在每个方法前面,因为我经常使用它。

除了像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏至、离别 2024-12-26 15:15:55

不,from ... import * 没有任何术语,除了 blah、bleh、bluh。您可以编写自己的导入函数来支持它,或者执行以下操作:

from xyz import *
del set

这将停止隐藏内置 set ,以便您可以再次使用它。然后,如果您需要 xyz.set 函数,您可以执行以下操作:

from xyz import set as xyzset

注意: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:

from xyz import *
del set

which will stop shadowing the built-in set so you can use it again. Then if you need the xyz.set function you can do:

from xyz import set as xyzset

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

树深时见影 2024-12-26 15:15:55

我想你想要的是:

from thing import a, b, c, d, e, f

将从 thing 导入 a, b, c, d, e, f

AFAIK,除了 a、b、c 之外,没有办法进行 from thing import
这就是为什么

import thing
thing.a

存在的首要原因。

I suppose what you want would be:

from thing import a, b, c, d, e, f

which will import a, b, c, d, e, f from thing.

AFAIK, there is no way to do an from thing import all but a, b, c
thats why

import thing
thing.a

exists in the first place.

辞别 2024-12-26 15:15:55

我不确定我完全理解发生了什么(给出实际的模块名称可能会有所帮助)。但通常认为 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, do from ... import thingA, thingB, thingC.

You could also do import ... as shortname, and then refer to the methods as shortname.whatever (where shortname could obviously be something very short).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文