Reason for not allowing import twice but allowing reload()

Chris Angelico rosuav at gmail.com
Mon Feb 29 11:05:24 EST 2016


On Tue, Mar 1, 2016 at 2:33 AM, BartC <bc at freeuk.com> wrote:
>> I think the OP's talking more about the situation of having an active
>> session (IDLE was mentioned), importing a local module (a .py file
>> from the current directory), then editing the file and re-importing,
>> which has no effect. While I am sympathetic to the problem, I don't
>> believe the language should be changed here; what might be useful is
>> something that notices that an already-loaded module is now out of
>> date, but really, the best solution is a change of workflow that no
>> longer has long-running modules loading live-updated code.
>
>
> Not in Python (this was pre-Python actually) but I once used such a
> technique all the time.
>
> The main set of modules would be running a GUI application which also had a
> command line.

I use this technique all the time, in current code. But not in Python.
The main reason is to have a server that permits clients to connect
and remain connected, and to update code in ways that affect those
connected clients. So by definition, I can't restart the server (it
would drop the connections), and the critical main loop doesn't
reload, but all the rest of the code does. It's pretty much the same
style you had.

Doing the same thing in Python would probably best be done using
"exec" rather than "import". It'd work, but you'd have to
differentiate in code between "load this module which can be reloaded"
and "import this module from the standard library" (where the latter
would be the regular "import" statement).

ChrisA


More information about the Python-list mailing list