简体   繁体   English

Python:导入模块一次用于整个包

[英]Python : import module once for a whole package

I'm currently coding an app which is basically structured that way : 我目前正在编写一个基本结构化的应用程序:

main.py main.py
+ Package1 + Package1
+--- Class1.py + --- Class1.py
+--- Apps + ---应用程序
+ Package2 + Package2
+--- Class1.py + --- Class1.py
+--- Apps + ---应用程序

So I have two questions : First, inside both packages, there are modules needed by all Apps, eg : re. 所以我有两个问题:首先,在两个包中,所有应用程序都需要模块,例如:re。 Is there a way I can import a module for the whole package at once, instead of importing it in every file that needs it ? 有没有办法可以一次导入整个包的模块,而不是在每个需要它的文件中导入它?

And, as you can see, Class1 is used in both packages. 而且,正如您所看到的,Class1用于两个包中。 Is there a good way to share it between both packages to avoid code duplication ? 有没有一种好方法在两个包之间共享它以避免代码重复?

I would strongly recommend against doing this: by separating the imports from the module that uses the functionality, you make it more difficult to track dependencies between modules. 我强烈建议不要这样做:通过将导入与使用该功能的模块分开,会使跟踪模块之间的依赖关系变得更加困难。

If you really want to do it though, one option would be to create a new module called common_imports (for example) and have it do the imports you are after. 如果你真的想这样做,一个选择是创建一个名为common_imports的新模块(例如)并让它执行你所追求的导入。

Then in your other modules, add the following: 然后在其他模块中添加以下内容:

from common_imports import *

This should give you all the public names from that module (including all the imports). 这应该为您提供该模块中的所有公共名称(包括所有导入)。

To answer your second question, if your two modules named Class1.py are in fact the same, then you should not copy it to both packages. 要回答第二个问题,如果您的两个名为Class1.py模块实际上是相同的,那么您不应该将它复制到两个包中。 Place it in a package which will contain only code which is common to both, and then import it. 将它放在一个包中,该包只包含两者共有的代码,然后导入它。 It is absolutely not necessary to copy the file and try to maintain each change in both copies. 绝对没有必要复制文件并尝试维护两个副本中的每个更改。

Q1: Q1:
You Must find a way to import your package, thus you have two choices: 必须找到导入包的方法,因此您有两种选择:
(Please correct me if I'm wrong or not thorough) (如果我错了或不彻底,请纠正我)

1. Look at James' solution, which you need to define a class, put all the modules inside and finally import them to your Sub-classes 1.查看James的解决方案,您需要定义一个类,将所有模块放入其中,最后将它们导入到您的子类中

2. Basically import nothing to your Main class, but instead, import only once to your Sub-classes 2.基本上不向您的Main类导入任何内容,而是只导入一次子类

For example:(inside A_1 subclass) import re
def functionThatUseRe(input): pass
例如:(在A_1子类内) import re
def functionThatUseRe(input): pass
import re
def functionThatUseRe(input): pass

Then inside your main class, just do try: from YourPackage import A_1 #windows except: import A_1 #MAC OSX
A_1.functionThatUseRe("")
然后在主类中, try: from YourPackage import A_1 #windows except: import A_1 #MAC OSX
A_1.functionThatUseRe("")
try: from YourPackage import A_1 #windows except: import A_1 #MAC OSX
A_1.functionThatUseRe("")

And you completely avoided importing modules multiple times 并且您完全避免多次导入模块

Q2: put your class1.py in the same directory with your main class, or move it to another folder, in Package1(&2).Apps Q2:将class1.py放在与主类相同的目录中,或将其移动到Package1(&2)中的另一个文件夹.Apps

import Class1

Start using the code from there 从那里开始使用代码

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

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