0

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

nwly
  • 1,360
  • 4
  • 16
  • 30
  • You could put them all in a separate file, and then import that file. But I am not sure there is much point in doing that, as now whoeever reads it cannot see what's in scope – Andrei Sep 12 '19 at 15:39
  • Possible duplicate of [Python: Importing an "import file"](https://stackoverflow.com/questions/6206204/python-importing-an-import-file) – CristiFati Sep 12 '19 at 15:40
  • It's similar but not exactly the same as the other thread. I'm trying to put the import statements inside functions of the utility python 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 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. – nwly Sep 12 '19 at 15:51

1 Answers1

0

With some slight modifications your method can work. In util.py

def import_usual_packages():
    global pd, np    # Make the names pd & np global
    import pandas as pd
    import numpy as np

And in main.py

import utils

utils.import_usual_packages()

utils.pd.DataFrame()  # access via the utils namespace

This is definitely not the cleanest approach though.

rdas
  • 20,604
  • 6
  • 33
  • 46
  • How can I change it so that I don't need to have the ``utils.`` namespace? I.e., so that in your last line I just need to call ``pd.DataFrame()``? – nwly Sep 12 '19 at 15:57
  • `from utils import pd` after the call to `import_usual_packages` – rdas Sep 12 '19 at 16:02