1

Hi so I’m working on a python script that involves a loop function, so far the loop function process is failing for some reason(although I kinda know why) but the problem I’ve got os.system(‘pause’) and also input(“prompt:”) at end of the code in order to pause all activity so I can read the error messages prior to script completion and termination but the script still shuts down, I need a way to HARD pause it or freeze before the window closes abruptly. Need help and any further insight.

Ps. Let me know if you need any more info to better describe this problem.

Emmanuel Batse
  • 153
  • 1
  • 6
  • 1
    What are you running the script in? Is there no way to view the error messages after the script finishes, like a log? – Chris May 31 '18 at 20:31

2 Answers2

3

I assume you are just 'double clicking' the icon on Window Explorer. This has the disadvantage which you are encountering here in that the shell (terminal window) closes when the process finishes so you can't tell what went wrong if it terminated due to an error.

A better method would be to use the command prompt. If you are not familiar with this, there are many tutorials online.

The reason this will help with your problem is that, once navigating to the script's containing directory, you can use python your_script.py (assuming python is in your path environmental variable) to run the script within the same window.

Then, even if it fails, you can read the error messages as you will only be returned to the command line.


An alternative hacky method would be to create a script called something like run_pythons.py which will use the subprocess module to call your actual script in the same window, and then (no matter how it terminates), wait for your input before terminating itself so that you can read the error messages.

So something like:

import subprocess
subprocess.call(('python', input('enter script name: ')))
input('press ENTER to kill me')
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
0

I needed something like this at one point. I had a wrapper that loaded a bunch of modules and data and then waited for a prompt to run something. If I had a stupid mistake in a module, it would quit, and that time that it spent loading all that data into memory would be wasted, which was >1min. For me, I wanted a way to keep that data in memory even if I had an error in a module so that I could edit the module and rerun the script.

To do this:

while True:
    update = raw_input("Paused. Enter = start, 'your input' = update params, C-C = exit")
    if update:
        update = update.split()
        #unrelevant stuff used to parse my update
    #custom thing to reload all my modules
    fullReload()
    try:
        #my main script that needed all those modules and data loaded
        model_starter.main(stuff, stuff2)
    except Exception as e:
        print(e)
        traceback.print_exc()
        continue
    except KeyboardInterrupt:
        print("I think you hit C-C. Do it again to exit.")
        continue
    except:
        print("OSERROR? sys.exit()? who knows. C-C to exit.")
        continue

This kept all the data loaded that I grabbed from before my while loop started, and prevented exiting on errors. It also meant that I could still ctrl+c to quit, I just had to do it from this wrapper instead of once it got to the main script.

Is this somewhat what you're looking for?

The answer is basically, you have to catch all your exceptions and have a method to restart your loop once you figured out and fixed the issue.

jeremysprofile
  • 10,028
  • 4
  • 33
  • 53
  • @Joe your first suggestion on running in the cmd window worked as well calling the process from a cmd terminal using python script.py thanks so much. So far I’m seeing a bunch of error messages involving my bad code and other request rejection events from the backend lol – Emmanuel Batse Jun 01 '18 at 07:30
  • you responded to the wrong answer. Also, if Joe's answer helped you, please accept it as the best answer for your question so others know this question is resolved. – jeremysprofile Jun 01 '18 at 17:50