Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - optimize by not importing at module level?

In a framework such as Django, I'd imagine that if a user lands on a page (running a view function called "some_page"), and you have 8 imports at the top of module that are irrelevant to that view, you're wasting cycles on those imports. My questions are:

  1. Is it a large enough amount of resources to make an impact on a high-traffic website?
  2. Is it such a bad practice to import inside of a function for this purpose that it should be avoided at said impact?

Note: This could be considered premature optimization, but I'm not interested in that argument. Let's assume, for the sake of practical theory, that this is a completed site with loads of traffic, needing to be optimized in every way possible, and the application code, as well as DB have been fully optimized by 50 PhD database admins and developers, and these imports are the only thing left.

like image 622
orokusaki Avatar asked Nov 02 '10 18:11

orokusaki


People also ask

How do you optimize imports in Python?

The Optimize Imports feature helps you remove unused imports and organize import statements in the current file or in all files in a directory at once according to the rules specified in Settings/Preferences | Editor | Code Style | <language> | Imports.

Should Python imports always be at the top?

Imports should always be written at the top of the file, after any module comments and docstrings. Imports should be divided according to what is being imported. There are generally three groups: standard library imports (Python's built-in modules)

What is a lazy import?

“Lazy” means that the import of a module (execution of the module body and addition of the module object to sys. modules ) should not occur until the module (or a name imported from it) is actually referenced during execution.

Does the order of imports matter in Python?

Import order does not matter. If a module relies on other modules, it needs to import them itself. Python treats each . py file as a self-contained unit as far as what's visible in that file.


2 Answers

No, don't do this. In a normal python execution environment on the web (mod_wsgi, gunicorn, etc.) when your process starts those imports will be executed, and then all subsequent requests will not re-execute the script. If you put the imports inside the functions they'll have to be processed every time the function is called.

like image 154
Alex Gaynor Avatar answered Nov 05 '22 22:11

Alex Gaynor


Yes, it is a bad practice to import at the function level. By using smarter imports at the top of the module, you create a one time, small cost. However, if you place an import in a function you will suffer the cost of the import each time that function is run. So, rather than import in the function, just import at the top of the module.

A few things you can do to clean up and improve your imports:

  • Don't use wild imports e.g. from x import *
  • Where possible, just use a normal import e.g. import x
  • Try to split your code up into smaller modules that can be called separately, so that fewer imports are made

Also, placing imports at the top of the module is a matter of style. There's a reason why PEP 8 says that modules need to be imported at the top. It's far more readable and maintainable that way.

Finally, some imports at function level will cause compatibility issues in the future, as from x import * is not valid Python 3.x at function level.

like image 24
Rafe Kettler Avatar answered Nov 05 '22 21:11

Rafe Kettler