简体   繁体   English

自动导入给定文件夹中的所有Python文件?

[英]Automatically import to all Python files in the given folder?

I am relatively quite new to Python and I try to learn the "Pythonic" way of doing things to build a solid foundation in terms of Python development. 我对Python比较陌生,我尝试学习“Pythonic”的做法,以便在Python开发方面建立坚实的基础。 Perhaps what I want to achieve is not Python at all, but I am nonetheless seeking to find out the "right" way to solve this issue. 也许我想要实现的不是Python,但我仍然试图找到解决这个问题的“正确”方法。

I am building an application, for which I am creating modules. 我正在构建一个应用程序,我正在创建模块。 I just noticed that a module of mine has 7 different .py Python files, all importing 3 different same things. 我只是注意到我的一个模块有7个不同的.py Python文件,所有这些都导入了3个不同的东西。 So all these files share these imports. 所以这些文件共享这些导入。

I tried removing them, and inserting these import to the empty init .py in the folder, but it did not do the trick. 我尝试删除它们,并将这些导入插入到文件夹中的空init .py,但它没有做到这一点。

If possible, since these imports are needed by all these module files, I would not like them to be imported in each file one by one. 如果可能的话,由于所有这些模块文件都需要这些导入,我不希望它们逐个导入到每个文件中。

What can I do to perform the common import? 我该怎么做才能执行常见的导入?

Thank you very much, I really appreciate your kind help. 非常感谢,非常感谢您的帮助。

As the Zen of Python states, "Explicit is better than implicit", and this is a good example. 正如PythonZen所说,“明确比隐含更好”,这是一个很好的例子。

It's very useful to have the dependencies of a module listed explicitly in the imports and it means that every symbol in a file can be traced to its origin with a simple text search. 在导入中明确列出模块的依赖关系非常有用,这意味着文件中的每个符号都可以通过简单的文本搜索跟踪到其原点。 Eg if you search for some_identifier in your file, you'll either find a definition in the file, or from some_module import some_identifier . 例如,如果在文件中搜索some_identifier ,您将在文件中找到定义,或者from some_module import some_identifier It's even more obvious with direct references to some_module.some_identifier . 直接引用some_module.some_identifier更加明显。 (This is also one reason why you should not do from module import * .) (这也是你不应该from module import *做的一个原因。)

One thing you could do, without losing the above property, is to import your three shared modules into a fourth module: 在不丢失上述属性的情况下,您可以做的一件事是将三个共享模块导入第四个模块:

#fourth.py
import first
import second
import third

then... 然后...

#another.py
import fourth

fourth.first.some_function()
#etc.

If you can't stomach that (it does make calls more verbose, after all) then the duplication of three imports is fine, really. 如果你不能忍受它(它确实使得电话更加冗长,那么)三个进口的重复是真的。

I agree with DrewV, it is perfectly pythonic to do 我同意DrewV,它完全是pythonic

File1: 文件1:

import xyz

import abc

... ...

File2: 文件2:

import xyz

An almost identical question has also been addressed in the following link: 以下链接也解决了几乎相同的问题:

python multiple imports for a common module python多个导入的常见模块

As it explains, Python does the job of optimising the module load, so you can write multiple import statements and not worry about performance losses, because the module is only loaded once. 正如它解释的那样,Python完成了优化模块负载的工作,因此您可以编写多个import语句而不用担心性能损失,因为模块只加载一次。 In fact, listing out all the imports in each file makes it explicitly clear what each file depends on. 事实上,列出每个文件中的所有导入使得它明确地清楚每个文件所依赖的内容。

And for a discussion of how imports interact with namespaces, see: 有关导入如何与命名空间交互的讨论,请参阅:

Python imports across modules and global variables Python跨模块和全局变量导入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM