Reason for not allowing import twice but allowing reload()

Chris Angelico rosuav at gmail.com
Mon Feb 29 02:11:33 EST 2016


On Mon, Feb 29, 2016 at 6:02 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> - if subsequent imports of same module in a session are not effective, why not simply flag those attempts as an error, rather than letting them go effect-less.
>
> Because there are legitimate reasons for importing the same module
> multiple times. For example, I have a script that imports the sys
> module. It also imports the os module, which imports the sys module.
> Both the main module of my script and the os module have need of the
> sys module, so they both import it. If subsequent imports of the
> module raised an exception, this wouldn't be possible.

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. I would,
instead, run a short-lived module and simply restart it after any edit
(possibly dropping to interactive mode at the end). Save, restart.
Save, restart. Python isn't optimized for the "keep running but change
code" execution model (for that, use Pike, and pay the complexity
overhead).

ChrisA



More information about the Python-list mailing list