Jamie
Jamie

Reputation: 589

Python equivalent of IDL's stop and .reset

I'm relatively new to python but have a bit of experience using IDL. I was wondering if anyone knows if there are equivalent commands in python for IDL's stop and .reset commands.

If I'm running some IDL script I wrote that I put a stop command in, essentially what it does is stop the script there and give me access to the command line in the middle of the script. So I have access to all the functions and variables that I defined before the stop command, which I find really useful for debugging.

The .reset command I find extremely useful too. What it does is reset the the IDL environment (clears all variables, functions, etc.). It's as if I closed that session and opened a new one, but without having to exit and restart IDL. I find that if I'm trying to debug a script I wrote it's useful sometimes to start from scratch and not have to reset IDL (or python now). It would be useful also in python to be able to un-import any modules I had previously imported.

Any help with these issues would be greatly appreciated.

Cheers

Related

Upvotes: 8

Views: 5617

Answers (8)

Syrtis Major
Syrtis Major

Reputation: 3929

An update to redacted's solution. The interactive IPython is much more powerful and convenient than pdb.set_trace.

Try this script

from IPython import embed
a = 1
b = 2
print('before')

embed()

print('after')
c = 3

Put embed() where you want to interrupt. Run the script you will enter an interactive IPython Shell, you can view and modify the variables. The script will continue after you exiting the shell.

Upvotes: 1

IdlToPython
IdlToPython

Reputation: 11

pdb.set_trace() breaking out of code apparently does not allow you to ".continue" (IDL command) from that point (from http://pythondammit.blogspot.fr/2012/04/equivalent-of-idls-stop-command.html)

Upvotes: 1

jakebrinkmann
jakebrinkmann

Reputation: 805

You could do %reset from within an IPython shell.

For stops, just add pydebug breakpoints as mentioned

Upvotes: 1

DarenW
DarenW

Reputation: 16906

Use pdb, as in this short script. Run it at the command line, and the PDB prompt will magically appear allowing single stepping, evaluation of arbitrary expressions, etc.

#!/usr/bin/env  python
import pdb;

print 1
print 2
pdb.set_trace()
print 3
print 4

Upvotes: 1

redacted
redacted

Reputation: 2429

IPython (aside from being a far nicer REPL than the standard python interpreter) may do what you want:

from IPython.Shell import IPShellEmbed

start_shell = IPShellEmbed()

def times_2(x):
    return 2*x

a = 5

start_shell()

# now in IPython shell
# a -> 5
# times_2(a) -> 10

Note that any changes you make in the shell will not be sent back to the main python process on exit - if you set a = 10 in IPython (using the above example), a is still equal to 5 in the main python process.

edit: post on IPython-user mailing list where I first saw this technique.

Upvotes: 3

PTBNL
PTBNL

Reputation: 6122

Welcome to the Python community! I'm still learning, but imo Python's nicer than the Interactive Data Language.

Anyway, Ignacio's answer about using the code module looks like it may provide what you want, at least as far as a parallel to IDL's stop.

Another thing you may find useful is to go into Python's interactive mode and import your program. You can then interact with it by running functions, etc. (Admittedly, I'm no expert at this.) If you do this, you'll need a main() function in the file which drives the program. For example, you'd have something like:

import sys
def main():
    # do stuff
    return(0)

if __name__ == '__main__':
    sys.exit(main())

instead of just:

# do stuff

This prevents the execution of the program when you pull it into the Python interpreter. For more, see Guido's article about main functions.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798636

stop sounds equivalent to use of the code module. .reset doesn't have an equivalent in Python short of gratuitous use of del.

Upvotes: 1

Hamish Grubijan
Hamish Grubijan

Reputation: 10820

You probably just want to use a Python debugger for this.

Upvotes: 0

Related Questions