当软件变得越来越大时,可能很难为每个“ .py”文件提供一个清晰的导入列表。因此,我想到的问题是,是否有任何最佳实践。为了进一步说明这个问题,假设我们有5个文件,它们使用sklearn,numpy等。现在,可以创建类似于我们在C ++中所做的那样的所谓的“ stdafx.py”文件,而不是使用以下方法在5个文件代码的开头导入每个软件包:import stdafx.py *
其中stdafx.py
是所有这些软件包的批处理。换句话说,它将包含:
-----------------------------stdafx.py------------------------------------
import numpy as np
import pandas as pd
from tensorflow import keras
from X import Y
.
.
.
您可以这样操作:
stdafx.py
import collections
import itertools
import functools
another_module.py
from stdafx import *
print(collections, itertools, functools) # all modules are available