8

How can I start REPL at the end of python script for debugging? In Node I can do something like this:

code;
code;
code;

require('repl').start(global);

Is there any python alternative?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Lapsio
  • 6,384
  • 4
  • 20
  • 26

2 Answers2

7

If you execute this from the command prompt, just use -i:

➜ Desktop echo "a = 50" >> scrpt.py
➜ Desktop python -i scrpt.py 
>>> a
50

this invokes Python after the script has executed.

Alternatively, just set PYTHONINSPECT to True in your script:

import os
os.environ['PYTHONINSPECT'] = 'TRUE'  
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
3

Just use pdb (python debugger)

import pdb
print("some code")
x = 50
pdb.set_trace() # this will let you poke around... try "p x"
print("bye")
Yoav Glazner
  • 7,936
  • 1
  • 19
  • 36