13

When importing modules in python, is there a memory difference when you import a single module from a package eg: from math import ceil vs the whole package eg: import math? I guess what I am really asking is whether or not it slows processing down when the script is run?

5
  • 1
    I think that this depends on how the package's __init__.py is configured ... but, python imports are generally fast and the memory footprint is generally small (unless you have some module level intense processing going on). It's probably not really worth worrying about unless you have some really stringent hardware constraints...
    – mgilson
    Jul 29, 2015 at 4:28
  • 1
    In python we generally write codes that make the file/module importable. Thus if you go on using import math or os in multiple files it will lead to code duplication and if will expose so many functions that you don't even know or you don't even want to import. Thus it is a good habit to use from os import path instead of import os.
    – Shrey
    Jul 29, 2015 at 4:46
  • 2
    @Shrey a running Python interpreter will import at most one instance of a module no matter how many times it is imported. The from x import y just promotes y to the current lexical scope; that is, whether you have to call it s math.sin() or just sin(). It otherwise has no effect at all on code duplication or even imported code. Thus, from math import sin still imports math.cos(), math.log() and all the rest.
    – msw
    Jul 29, 2015 at 6:06
  • 1
    @msw Thanks. So if we want sin() function to be called many a times we should opt for from math import sin. Just thinking should we treat this as a feature or a bug?
    – Shrey
    Jul 29, 2015 at 6:31
  • 1
    It is definitely a feature, but its overuse is error-prone. There is a tiny extra cost in that math.sin takes two dictionary lookups instead of one, however if the top-level namespace becomes very full you'll probably lose that tiny gain in looking through a much larger namespace. But this is Python, so nano-performance ought not be a priority. If I am writing a trigonometry heavy program then from math import * may well make my code easier to read. In general though, module.object or even module.submodule.object is easier on the reader and that's what counts.
    – msw
    Jul 29, 2015 at 14:42

1 Answer 1

28

There is no difference between those. Raymond Hettinger, one of the core Python developers, recently tweeted this:

#python tip: from-imports don't save memory. They execute and cache the entire module just like a regular import.

2
  • 7
    Then why have from-imports at all?
    – ajsp
    Jul 29, 2015 at 5:05
  • 15
    @ajsp so that you do not have to access things as - module.name ? Jul 29, 2015 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.