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 has changed: unimport foo <-- How do I do this? import foo myfoo = foo.Foo()
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can reload a module when it has already been imported by using
importlib.reload():In Python 2,
reloadwas a builtin. In Python 3, it was moved to theimpmodule. In 3.4,impwas deprecated in favor ofimportlib. When targeting 3 or later, either reference the appropriate module when callingreloador import it.I think that this is what you want. Web servers like Django’s development server use this so that you can see the effects of your code changes without restarting the server process itself.
To quote from the docs:
As you noted in your question, you’ll have to reconstruct
Fooobjects if theFooclass resides in thefoomodule.