1

I have a Python projects with many .py scripts. Most of the scripts have a long like of similar import statements. What is the general convention of structuring such imports?

  1. Would they be repeated in each of the .py files?
  2. Would they all be listed in a separate file and would that file be imported in each of the .py files?

If it helps, my imports look like these, and there are a few more.

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
2
  • 3
    first version - when you open file then you see all needed modules. Commented May 20, 2019 at 22:20
  • @furas, thanks, quick clarification- I would have about 20-30 lines of import statements in each of my files. Is that okay? Commented May 20, 2019 at 22:23

2 Answers 2

1

The definitive source for things like this is the PEP 8 style guide. Specific to your situation, there is a subsection for imports, which states as follows (emphasis mine):

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...

Which would indicate that best practice is to do as you stated in option (1).

As an anecdote, if I (or you) have to edit one of your .py files down the road, I want to see each import in the expected format, so I know the dependencies no matter which script file I open

Sign up to request clarification or add additional context in comments.

Comments

1

I would advise to place imports that a file needs in that file. If a module happens to be needed in several files - that's ok. Python only loads a module once. The only thing I found noteworthy of having the same module imported in my custom module, and main (main also imports my custom api module) is when checking exceptions:

My api.py has import pycurl My main.py has import api And also it used to have import pycurl

I was able to identify pycurl exceptions like

...
except pycurl.error as e:

Later I removed import of pycurl from my main.py And the above code no longer worked. Instead I had to do:

...
except api.pycurl.error as e:

Hope this helps

Edit: You have a lot of imports from sklearn, and some are repeated. While it's not critical, it does impede readability of your code. Maybe you should just import sklearn by itself, and call its components later in the code like sklearn.naive_bayes.GaussianNB it will have the added benefit of showing what objects belong to sklearn in the code, at the expense of a little more typing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.