IPython: run the script starting at a specific line

I am writing a script interactively with IPython. This is what I am doing now:

  • write a piece of code
  • run in ipython using "run -i file_name.py".
  • make changes and repeat 2 until I think that everything is in order.
  • Comment out the entire previous snippet.
  • write a new code snippet based on the previous one.
  • return to step 2.
  • ......

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

+6
source share
6 answers

I would personally use an ipython laptop, but you also call it your favorite text editor and always copy the piece of code you want to run, and use the% paste magic command to run that fragment in the ipython shell. He will take care of you.

+1
source

IPython Notebook lets you interactively run scripts line by line. It comes with IPython, just run:

ipython notebook 

from the terminal to start it. Its web interface is for IPython, where you can save laptops to *.py files by clicking save as in settings.

Here is more information about this video .

0
source

Use the magic of %edit stuff.py (use first) and %ed -p (after first use) and it will get your $EDITOR inside ipython. After exiting the editor, ipython will run the script (if you did not call %ed -x ). This is the fastest way to find work in CLI-ipython. Laptops are good, but I like to have a real editor for the code.

0
source

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

Suppose you want to run the script "foo.py" from lines 18 through 23 . You want to start like this:

 ipdb foo.py 

Now let's j move to line 18 (i.e. ignore all lines until the 18th):

 ipdb> j 18 

Then we set reakpoint b on line 23 (we don't want to go any further):

 ipdb> b 23 

Finally, let it run:

 ipdb> c 

The task is completed :)

0
source

(Based on lev answer)

From the interactive shell:

 %run -i -d foo.py 

should then enter the debugger and continue:

 j <line_number> c 

and etc.

EDIT: Unfortunately, this is similar to debugging the ipython magic% debug command.

0
source

For quick and flexible use of http://qtconsole.readthedocs.io/en/stable/

It looks like a Jupyter laptop based on your browsers (as pointed out by @agonti and @ magellan88, but presumably much faster and also has emacs style keyboard shortcuts.

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

0
source

Source: https://habr.com/ru/post/972352/


All Articles