1

I have a Flask backend in which I import the magic module on module level, but I use it only in one function:

import magic

def wizard(filename):
    return magic.from_file(filename)

As magic has a dependency to a C library which I can't install on windows and as this function is the only place where magic is used, I wondered if there is a drawback (or even an advantage) of moving the import to the function:

def wizard(filename):
    import magic
    return magic.from_file(filename)

Would the overhead of importing magic only matter for the first user who uses the function and later it's there for everybody? Or does Flask / Python load it again and again?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • Module importing as such only happens once, next time you call the function Python will just check the module is already in the collection of imported modules ([`sys.modules`](https://docs.python.org/3/library/sys.html#sys.modules)) and take it from there; to actually reload a module you would use something like [`importlib`](https://docs.python.org/3/library/importlib.html). So there may be a very slight overhead but surely negligible. Loading within the function can help reduce startup time, only loading additional modules "as needed" (lazily). In any case a benchmark should confirm that. – jdehesa Oct 10 '20 at 09:04
  • @jdehesa "Module importing as such only happens once" - is that also the case in a threaded context of a web server? – Martin Thoma Oct 10 '20 at 09:05
  • @jdehesa "In any case a benchmark should confirm that" - how would you benchmark this for a web server? I want to know if every single user pays that performance fee or if only the first one pays it. – Martin Thoma Oct 10 '20 at 09:06
  • I don't think threading makes a difference here, as long as everything is in the same process. By benchmarking, I meant just timing the runtime of a function with the import and without it (e.g. with IPython's `%timeit`) - though the difference may be too small to measure reliably. – jdehesa Oct 10 '20 at 11:05
  • Some related questions: [this](https://stackoverflow.com/q/128478/1782792), [this](https://stackoverflow.com/q/3095071/1782792) and [this](https://stackoverflow.com/q/1024049/1782792). – jdehesa Oct 10 '20 at 11:09

0 Answers0