[Django]-Django: is importing a view from a module slower than including it in the main views.py file?

2👍

As the others have already pointed out your examples won’t make any difference as the import will only happen once.
More important is, that you should make sure not to do eg. any imports inside a function if it that is avoidable, as, these imports will be redone every time the function is executed!

If you want to have clear and maintable code, it is far more important to take care of the structuring of your imports and, a wildcard import like from app_name.models import * is pretty evil, you should avoid it!
See also eg. this post!

5👍

You won’t see any difference in speed. Once a module is imported, it is kept in memory so it doesn’t have to be loaded again. You can check what has been loaded like so:

import sys
print sys.modules
# {'cStringIO': <module 'cStringIO' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/cStringIO.so'>, 'copy_reg': <mo........

3👍

The very act of importing something “slows” the application down. However, we’re talking about the time it takes to read the file from the filesystem and compile it once — milliseconds. So, yes, importing the view versus the view just being there is slower, but practically, there’s no difference.

Leave a comment