Let's say I have a script1.py with imports.
假设有一个script1。py和进口。
How can I share script1.py imports with script2.py? without copy and pasting.
如何共享script1。py和script2.py进口吗?没有复制和粘贴。
2 个解决方案
#1
2
Your application should consist of many modules that are logically independent. It makes your program easier to read, debug etc. Just like Python's standard library is divided into many modules (as you said - you have to import things from many of them).
应用程序应该包含许多逻辑上独立的模块。它使您的程序更容易阅读、调试等等。就像Python的标准库被划分为许多模块(正如您所说的那样,您必须从其中许多模块中导入内容)。
The more modules you have, the fewer imports you need to do in each of them. It's because each separate part of your program only imports what it needs. Thanks to this attitude you and other programmers can easily understand the code.
您拥有的模块越多,您需要在每个模块中进行的导入就越少。这是因为程序的每个独立部分只导入它需要的内容。由于这种态度,您和其他程序员可以很容易地理解代码。
Another way to deal with it (not really recommended):
另一种处理方法(不推荐):
imports.py: (in this example I assume you put it in the same directory as test.py)
进口。py:(在本例中,我假设您将它放在与test.py相同的目录中)
from datetime import date
from re import *
test.py:
test.py:
from imports import * #you import your imports
today = date.today() #using what you imported in imports.py
print(today)
It might be dangerous due to possibly same names imported. Be careful!
这可能是危险的,因为可能导入了相同的名称。小心!
#2
-1
though I agree with the comments... you can have a module that defines the all attribute
虽然我同意这些评论……您可以有一个定义all属性的模块
from crazyapp import all, my, imports
__all__ =['all', 'my', 'imports']
then just call to import the modules.
然后调用来导入模块。
from allmyimports import *
从allmyimports进口*
But fix things if you have that many.... 200? Think of a module as something that does one thing and does that one thing good. you'll find your imports will drop for the most part.
但解决问题如果你有许多....200年?把模块想象成做一件事,做一件好事的东西。您将发现您的导入将在很大程度上下降。
#1
2
Your application should consist of many modules that are logically independent. It makes your program easier to read, debug etc. Just like Python's standard library is divided into many modules (as you said - you have to import things from many of them).
应用程序应该包含许多逻辑上独立的模块。它使您的程序更容易阅读、调试等等。就像Python的标准库被划分为许多模块(正如您所说的那样,您必须从其中许多模块中导入内容)。
The more modules you have, the fewer imports you need to do in each of them. It's because each separate part of your program only imports what it needs. Thanks to this attitude you and other programmers can easily understand the code.
您拥有的模块越多,您需要在每个模块中进行的导入就越少。这是因为程序的每个独立部分只导入它需要的内容。由于这种态度,您和其他程序员可以很容易地理解代码。
Another way to deal with it (not really recommended):
另一种处理方法(不推荐):
imports.py: (in this example I assume you put it in the same directory as test.py)
进口。py:(在本例中,我假设您将它放在与test.py相同的目录中)
from datetime import date
from re import *
test.py:
test.py:
from imports import * #you import your imports
today = date.today() #using what you imported in imports.py
print(today)
It might be dangerous due to possibly same names imported. Be careful!
这可能是危险的,因为可能导入了相同的名称。小心!
#2
-1
though I agree with the comments... you can have a module that defines the all attribute
虽然我同意这些评论……您可以有一个定义all属性的模块
from crazyapp import all, my, imports
__all__ =['all', 'my', 'imports']
then just call to import the modules.
然后调用来导入模块。
from allmyimports import *
从allmyimports进口*
But fix things if you have that many.... 200? Think of a module as something that does one thing and does that one thing good. you'll find your imports will drop for the most part.
但解决问题如果你有许多....200年?把模块想象成做一件事,做一件好事的东西。您将发现您的导入将在很大程度上下降。