Most of my jupyter notebooks usually begin with a long list of common packages to import. E.g.,
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
...
Is there any way I can call all of the same packages using a function defined in another python file?
For example, I tried putting the following in util.py
:
def import_usual_packages():
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
And in the main notebook:
import util
util.import_usual_packages()
so that the end effect is I can still call the usual packages without using additional namespaces, e.g.
pd.DataFrame()
At the moment this wouldn't work with what I have above. E.g. pd
isn't defined.
EDIT: It's similar but not exactly the same as other questions asking about how to do a from util import *
. I'm trying to put the import statements inside functions of the utility python file, not simply at the top of the file. I'm aware that I can simply put e.g. import pandas as pd
at the top of the util file and then run from util import *
, but that is not what I'm looking for. Putting it within functions gives me additional control. For example, I can have 2 different functions called import_usual_packages()
and import_plotting_packages()
within the same util file that calls different groups of packages, so that in my notebook I can simply call
import_usual_packages()
import_plotting_packages()
instead of having 10+ lines calling the same stuff everytime. This is purely for personal use so I don't care about if other people don't understand what's going on (in fact, that might be a good thing in some circumstances).