Skip to content

re: How Specific Are you With Your Imports and Why? VIEW POST


from mymodule import funca
from mymodule import funcb
from somemodule import afunc

I always use this syntax with the exception of pandas and numpy which I import as import pandas as pd and import numpy as np, repectively. The reason is not performance or security but other three reasons.

First, the fact that the autocompletion in Jupyter Notebooks (my default development environment) is not working all the time.

Second, if you use the * syntax you have no way of knowing what you actually import: If you write from mymodule import *, you could have potentially imported a lot of funcions that you don't want or need.

Third, it saves you some typing later on. If you write from sklearn.metrics import mean_squared_error in the beginning of the file (or wherever you do your imports), you later only have to write mean_squared_error(), if you call the function.


I have heard this method used many times before and that is what I was wondering! For me, it usually depends on the situation and how familiar I am with what I am working with. If i'm doing something I've done a million times, I typically just import the exact functions I need from a given class. But if I'm not sure, I'll bring in an entire class or even an entire module. Even in times when I DO know exactly what is going on, I'll bring in most of the class. Example being:

from bs4 import BeautifulSoup

I am also wondering how developers in other languages approach this kind of thing. For instance, when I was a C# developer we rarely imported and entire class and NEVER an entire namespace simply because system.web and the like were so humongous that it would take FOREVER to compile. Python tends to be a lot lighter and modular so I've not experienced that kind of specificity in it despite almost a decade using it. Thanks for the input! I love hearing the way other devs do things because there is always something I can take away from the tried and true practices of others!

code of conduct - report abuse