2

I have a lot of import statements in my views.py.

Does it help in terms of performance if instead putting all of the imports at the beginning of the file I put the imports in the views exactly where they are needed?

Having them in the particular view where they are needed helps also if you for example have to remove a view you don't have to scroll up and remove the unneeded imports.

Al Bundy
  • 1,599
  • 1
  • 14
  • 20

1 Answers1

1

Does it help in terms of performance if instead putting all of the imports at the beginning of the file I put the imports in the views exactly where they are needed?

No. If all imports will be placed at the beginning of the file, the import is performed only once - when the django-process starts.

defuz
  • 26,721
  • 10
  • 38
  • 60
  • And what if I put the imports in the view functions? Does this mean that the Python tries to import (but doesn't) every-time the view is called? – Al Bundy Oct 11 '12 at 19:28
  • If shortly: The module will be imported one time, but the function will be performed a little slower. See detailed answer here: http://stackoverflow.com/questions/128478/should-python-import-statements-always-be-at-the-top-of-a-module – defuz Oct 11 '12 at 19:33