Why are packages like glob
and psycopg2
not shortened on import? Is there any other reason than convention or personal preference? Perhaps best practice?
Example: For the sake of readability and reducing the overhead of loading an entire package:
# Best practice
from sklearn.metrics import r2_score
model_score = r2_score(arg1, arg2)
# Similarly, to keep names short
import sklearn
model_score = sklearn.metrics.r2_score(arg1, arg2)
Similarl example: long package names are just shortened, especially if many part of the package are used
# Best practice
import pandas as pd
import numpy as np
import seaboard as sns
import matplotlib.pyplot as plt
df = pd.DataFrame(dictobject)
w_avg = np.average(mylist, weights=w)
sns.heatmap(df)
plt.show()
# instead of
import pandas
import numpy
import seaboard
import matplotlib
df = pandas.DataFrame(dictobject)
w_avg = numpy.average(mylist, weights=w)
seaborn.heatmap(df)
matplotlib.pyplot.show()
Why do we not do the same with glob?
# Shouldn't this be best practice?
import glob.glob as gl
jpgs = gl('path/*.jpg')
# But instead this seems more prominent:
import glob
jpgs = glob.glob('path/*.jpg')
Perhaps in case if we needed one of glob.glob
's lesser know cousins (glob.iglob
or glob.escape
), we could import glob as gl
(and then use gl.iglob
or gl.escape
).
This question: python import module vs import module as, did not answer my question.