简体   繁体   中英

How to prevent python IDLE from restarting when running new script

I use Python 3.6.1 IDLE. I'd like to be able to run a sequence of scripts while maintaining the local environment (variables etc.) just as if I had entered all the commands line by line into the shell. However, whenever I execute a script the environment restarts and I lose all the variables from previous executions.

Is there anything I can do to prevent this restart? I don't want to have to type things line by line in the shell.

This question is similar to Run a module in IDLE (Python 3.4) without Restart but there's no convincing answer yet, maybe because the question is worded confusingly.

Edit: I'll give some background to make clearer why I want a second script to be run that shares state with the first one. I'm using Selenium Web Driver to forward emails from my University webmail account. If I log in too often, Outlook Web App realizes that something odd is going on and blocks access. So I want to do the log-in once with the first script and then be able to test different forwarding scripts on top of this first one. So what I can do, is run the first script and then type the second one into the shell one by one. But that's really annoying. It would be neat if I could just run a second script as if it was typed into the shell, ie without restart.

What about writing a master script that makes calls to execfile("other_script.py") (in Python 2) or exec(open("other_script.py").read()) (Python 3)? Then just run the master script in IDLE. You will probably need shebang lines and (possibly) appropriate execute permissions for each script.

See this thread as I derived my answer from some stuff discussed there.

Another option would be to automate your "typing things into the shell" with a shell script (bash, batch, sh, whatever shell you are using on your system).

Realistically though, like others have said, it doesn't really seem like there is much benefit in modularizing this task by splitting it up into separate files. Why not just create a Python program that has all the functions you need, and then calls them as appropriate?

Added June 2019: On the editor Run menu, Run... Customized opens a dialog with [X] Restart . Uncheck that box and the restart is skipped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM