Is it necessarily safe to delete unused imports in borrowed code?
Question: I'm studying a demo script from the
[paramiko](https://github.com/paramiko/paramiko) package
([forward.py](https://github.com/paramiko/paramiko/blob/master/demos/forward.py))
to familiarize myself with SSH tunneling in Python. When I opened the demo
script in my IDE, I noticed some unused import warnings:

    
    
    import getpass
    import os        # <--- IDE warning: "'os' imported but unused"
    import socket    # <--- IDE warning: "'socket' imported but unused"
    import select
    import SocketServer
    import sys
    from optparse import OptionParser
    

And sure enough, neither module is explicitly referenced in the script. I'm a
neat freak, so I want to delete this clutter - but I'm also inexperienced and
can't tell for certain if they're extraneous.

The most straightforward thing for me to do would be to delete the imports and
then try to run the script without them, but I don't know that I would be able
to tell if something wasn't working correctly since I'm still trying to
understand how the script works.

I found [a conversation](https://mail.python.org/pipermail/python-
list/2013-May/646311.html) that made me think there might be a reason to
import a module without using it. But in [investigating
further](https://docs.djangoproject.com/en/1.3/topics/http/views/) it seems to
me that the example in that conversation has nothing to do with the
functionality of the code, and the user is just trying to follow a stylistic
convention.

How can an import be justified if there is never a reference to whatever was
imported? I guess there might be code that runs on import, e.g.:

    
    
    """
    silly.py: this is very silly
    """
    
    class ManBearPig(Man, Bear, Pig):
        def __init__(self, name):
            self.name = name
    
        @classmethod
        def summon(cls):
            """summon Gary"""
            global Gary
            Gary = cls('Gary')
    
    ManBearPig.summon()
    

That seems like a terrible idea, but I'm not confident enough in my python-fu
to be sure. `socket` is already imported within `SocketServer` so what's the
point of importing it here if you're not using it explicitly?


Answer: Yes, it is safe enough to remove imports that your code doesn't use in the
current module, or that do not themselves have side effects.

Python imports modules _once_ and stores them in `sys.modules` for future
reference; subsequent imports will re-use the already imported module object.
Modules that have side effects make use of that, but they still have to be
imported that one time.

For 'regular' modules, all you have is an extra reference to the module object
or to objects within that module. That takes a modicum of extra memory (apart
from the module object itself), so the cost isn't that high.

But you then do have to track what modules you have used, and what global
names therefore can be in use in your code that could lead to bugs.

