Do unused imports in Python hamper performance?

Question:

Is there any effect of unused imports in a Python script?

Asked By: Aashish P

||

Answers:

You pollute your namespace with names that could interfere with your variables and occupy some memory.
Also you will have a longer startup time as the program has to load the module.

In any case, I would not become too neurotic with this, as if you are writing code you could end up writing and deleting import os continuously as your code is modified. Some IDE’s as PyCharm detect unused imports so you can rely on them after your code is finished or nearly completed.

Answered By: joaquin

“Unused” might be a bit harder to define than you think, for example this code in test.py:

import sys
import unused_stuff
sys.exit(0)

unused_stuff seems to be unused, but if it were to contain:

import __main__
def f(x): print "Oh no you don't"
__main__.sys.exit = f

Then running test.py doesn’t do what you’d expect from a casual glance.

Answered By: Flexo