Python equivalent of stopping IDL and .reset

I am relatively new to python, but have a little experience using IDL. I was wondering if anyone knows if python has equivalent commands for the IDL stop and .reset commands.

If I run some IDL script, I wrote that I entered the stop command, basically what it does is stop the script and give me access to the command line in the middle of the script, So I have access to all functions and variables, which I defined before the stop command, which are really useful for me for debugging.

The .reset command, which I find very useful. What it does is reset the IDL environment (clears all variables, functions, etc.). As if I closed this session and opened a new one, but without the need to end and restart the IDL. I find that if I try to debug a script, I wrote that it is sometimes useful to start from scratch and not have reset IDL (or python now). It would also be useful in python to be able to not import any modules that I previously imported.

Any help with these questions would be greatly appreciated.

Greetings

Related

  • Python Drop in REPL
  • Is it possible to switch to ipython from code?
+7
python reset equivalent idl-programming-language
source share
8 answers

IPython (besides being a lot better REPL than the standard python interpreter) can 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 

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

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

+3
source share

stop sounds equivalent to using the code module. .reset has no equivalent in Python, but without using del .

+1
source share

Use pdb, as in this short script. Run it on the command line, and the PDB tooltip will be magically displayed, 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 
+1
source share

pdb.set_trace() exception to the code does not seem to allow you to β€œ.continue” (the IDL command) from this point (from http://pythondammit.blogspot.fr/2012/04/equivalent-of-idls-stop- command.html )

+1
source share

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

0
source share

Welcome to the Python community! I'm still involved, but imo Python is better than an interactive data language.

In any case, Ignacio will reply that using the code module looks like it can provide what you want, at least to the parallel with the IDL stop.

Another useful thing is to go into Python interactive mode and import your program. Then you can interact with it by launching functions, etc. (Admittedly, I'm not an expert on this.) If you do, you will need the main () function in the file that controls the program. For example, you 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 program from executing when ported to the Python interpreter. See Guido's article on the main features for more details.

0
source share

You can do %reset from IPython shell.

For stops just add pydebug breakpoints as indicated

0
source share

Update to a fixed solution. Interactive IPython is much more powerful and convenient than pdb.set_trace.

Try script

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

Put embed() where you want to abort. Run the script, you will enter the IPython interactive shell, you will be able to view and modify the variables. The script will continue after exiting the shell.

0
source share

All Articles