8

I am writing my script interactively with IPython. This is what I currently do:

  1. write a chunk of code,
  2. run in ipython with "run -i file_name.py".
  3. make changes and repeat 2 until I think it is OK .
  4. comment out the entire previous chunk.
  5. write new chunk of code that is based on the previous one.
  6. go back to step 2.
  7. ......

Is there more efficient way? Can I start a script from a specific line while using all the variables in current namespace?

6 Answers 6

3

Use ipdb ("pip install ipdb" on the command line to install it).

Suppose you want to run script "foo.py" from line 18 to 23. You'll want to start like this:

ipdb foo.py

Now, let's jump to line 18 (i.e., ignore all the lines before the 18th):

ipdb> j 18

Next, we set a breakpoint at line 23 (we don't want to go further):

ipdb> b 23

Finally, let's execute:

ipdb> c

Job done :)

Sign up to request clarification or add additional context in comments.

Comments

2

(Based on lev's answer)

From the interactive shell:

%run -i -d foo.py

should then enter the debugger, and proceed with:

j <line_number>
c

etc.

EDIT: unfortunately, this seems to sort of break ipython's magic %debug command.

Comments

1

I'd personally also use the ipython notebook, but you call also use you favorite text editor and always copy out the chunk of code you want to run and use the magic command %paste to run that chunk in the ipython shell. It will take care of indentation for you.

Comments

1

Use the magic of %edit stuff.py (first use) and %ed -p (after the first use) and it will invoke your $EDITOR from inside of ipython. Upon exiting from the editor ipython will run the script (unless you called %ed -x). That is by far the fastest way I found to work in CLI-ipython. The notebooks are nice, but I like having a real editor for code.

Comments

0

An IPython Notebook allows you to interactively run scripts line by line. It comes with IPython, just run:

ipython notebook 

from the terminal to launch it. Its a web interface to IPython, where you can save the notebooks to *.py files by clicking save as in the settings.

Here's some more info from this video.

4 Comments

isn't it a web based tool? I need something that can run fairly fast.
its a server that communicates with a web interface, so its still running on the IPython kernel. Its equally as powerful as running IPython from the command line. I use it personally for all of my machine learning projects.
Thanks, I just checked out the demo on its front page, very nice tool. but doesn't it lose all the nice vim functionaries writing code on webpage?
Vim functionalities? Its a fully competent editor, if that's what your asking. You can also execute anything from the command line within the notebook.
0

For something fast as well as flexible use http://qtconsole.readthedocs.io/en/stable/

It is similar to the Jupyter notebook based on your browsers (as pointed out by @agonti and @magellan88, but presumably much faster. It also has emacs style keybindings.

I use ipdb, ipython, comupled with tmux and vim and get almost IDE like features and much faster.

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.