41

I am often asked to debug Python scripts written by others. I would like to send these scripts to IPython so it will drop into an IPython shell at the point the script fails.

Unfortunately, I cannot find a way to send (required) command-line options required by the scripts.

IPython assumes everything in is for IPython when I pass the script and its options as:

ipython <script_name> <script_options>

Is there a solution or workaround?

5 Answers 5

52
ipython -- sometest.py 1 2 3 4
Sign up to request clarification or add additional context in comments.

4 Comments

It works, but I could not find this in the docs, can you point me to where this is documented?
Also to remain within the interactive shell use ipython -i -- sometest.py 1 2 3 4 syntax.
Note that '--' is a feature of the system shell interpreter, and is not specific to ipython shell
If you like to debug with ipdb, you can similarly use ipython --pdb -- somtest.py 1 2 3 4.
32
ipython -i -c "%run test.py 1 2 3 4"

6 Comments

Very nice! Unfortunately, when it exceptions out, it drops me back onto the OS command line, not onto the IPython prompt. Suggestions?
Well you could just start ipython and then do %run test.py 1 2 3 4
Actually adding the -i makes it stay in the shell.
@jimbob If you don't mind me asking, where'd you find the '-i' option? I'm having no luck finding it in 'ipython -help' nor 'ipython.scipy.org/doc/stable/html/interactive/reference.html'
@JS I only found it in man python (not ipython): "-i When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command. It does not read the $PYTHONSTARTUP file. This can be useful to inspect global variables or a stack trace when a script raises an exception." Kinda frustrating it wasn't in man ipython, but gave it a shot and it worked.
|
6

I know there's an already accepted solution, but in the most recent version of ipython this won't work. Here's a cut and paste of the command I use to run tornado tests with --autoreload

ipython --c="%run test.py --autoreload"

This is using ipython .11.

Comments

1

Simple example here.

script.py

from sys import argv

script, first, second, third = argv

print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third

shell:

$ ipython script.py stuff things that
The script is called: ex13.py
Your first variable is: stuff
Your second variable is: things
Your third variable is: that

Comments

0

Many aspects of IPython's behavior can be controlled via settings in the user's IPython config files, which typically are in ~/.ipython/. A user can create multiple profiles, each with different settings of the config parameters. Each profile has its settings in a separate folder in the .ipython folder. The default profile is in profile_default, and the main file in there for customizing behavior is ipython_config.py. By default, it is almost entirely commented, with commented lines showing the config variables and their default settings. Uncomment or insert lines to alter behavior.

To change how IPython behaves at the end of running a script, use:

c.TerminalIPythonApp.force_interact = True

Then when the script ends (or raises an exception), IPython will keep running and present you with a prompt. This is the same behavior as ipython -i.

I use this setting in my default profile, because this is the way I always want IPython to behave. If that's not the case for you, you could create a profile with this behavior, to use just when you want this behavior. Or just keep using the (evidently undocumented) -i option.

IPython configuration documentation is available here: Introduction to IPython configuration — IPython documentation, with the force_interact option described here: Terminal IPython options — IPython documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.