Batman Batman - 1 year ago 66
Python Question

Most Performant Way To Do Imports

From a performance point of view (time or memory) is it better to do:

import pandas as pd


from pandas import DataFrame, TimeSeries

Does the best thing to depend on how many classes I'm importing from the package?

Similarly, I've seen people do things like:

def foo(bar):
from numpy import array

Why would I ever want to do an import inside a function or method definition? Wouldn't this mean that import is being performed every time that the function is called? Or is this just to avoid namespace collisions?

Answer Source

This is micro-optimising, and you should not worry about this.

Modules are loaded once per Python process. All code that then imports only need to bind a name to the module or objects defined in the module. That binding is extremely cheap.

Moreover, the top-level code in your module only runs once too, so the binding takes place just once. An import in a function does the binding each time the function is run, but again, this is so cheap as to be negligible.

Importing in a function makes a difference for two reasons: it won't put that name in the global namespace for the module (so no namespace pollution), and because the name is now local, using that name is slightly faster than using a global.

If you want to improve performance, focus on code that is being repeated many, many times. Importing is not it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download