I have a long-running Python server and would like to be able to upgrade a service without restarting the server. What\'s the best way do do this?
if foo.py          
        For Python 2 use built-in function reload():
reload(module)
For Python 2 and 3.2–3.3 use reload from module imp:
import imp
imp.reload(module)
But imp is  deprecated since version 3.4 in favor of importlib, so use:
import importlib
importlib.reload(module)
or
from importlib import reload
reload(module)
                                                                        If you are not in a server, but developing and need to frequently reload a module, here's a nice tip.
First, make sure you are using the excellent IPython shell, from the Jupyter Notebook project. After installing Jupyter, you can start it with ipython, or jupyter console, or even better, jupyter qtconsole, which will give you a nice colorized console with code completion in any OS.
Now in your shell, type:
%load_ext autoreload
%autoreload 2
Now, every time you run your script, your modules will be reloaded.
Beyond the 2, there are other options of the autoreload magic:
%autoreload
Reload all modules (except those excluded by %aimport) automatically now.
%autoreload 0
Disable automatic reloading.
%autoreload 1
Reload all modules imported with %aimport every time before executing the Python code typed.
%autoreload 2
Reload all modules (except those excluded by %aimport) every time before
executing the Python code typed.