gpt4 book ai didi

python - 在Python中构造重复的导入语句

转载 作者:行者123 更新时间:2023-12-01 00:56:15 31 4
gpt4 key购买 nike

我有一个包含许多 .py 脚本的 Python 项目。大多数脚本都有很长的类似导入语句。构建此类导入的一般惯例是什么?

  1. 它们会在每个 .py 文件中重复吗?
  2. 它们是否都会列在单独的文件中,并且该文件是否会导入到每个 .py 文件中?

如果有帮助的话,我的导入看起来像这些,还有更多。

import datetime
import logging
import random
import sys
import os
import operator
import warnings

import json
import numpy as np
import parseJSON
import pandas as pd


from sklearn.svm import SVC
from sklearn import svm
from sklearn import linear_model

from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble.forest import RandomForestClassifier
from sklearn.ensemble import BaggingClassifier

from sklearn.multiclass import OneVsRestClassifier
from sklearn.linear_model import SGDClassifier
from sklearn import neighbors
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.preprocessing import LabelEncoder

最佳答案

此类内容的权威来源是 PEP 8 style guide 。针对您的情况,有一个小节 for imports ,其说明如下(强调我的):

Imports should be grouped in the following order:

Standard library imports. Related third party imports. Local application/library specific imports. You should put a blank line between each group of imports.

Absolute imports are recommended, as they are usually more readable and tend to be better behaved...

这表明最佳实践是按照选项 (1) 中所述进行操作。

作为一个轶事,如果我(或你)必须编辑你的一个 .py 文件,我希望以预期的格式看到每个导入,所以无论我打开哪个脚本文件,我都知道依赖关系

关于python - 在Python中构造重复的导入语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56228847/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com