Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drop into python interpreter while executing function

Tags:

python

i have a python module with a function:

def do_stuff(param1 = 'a'):
    if type(param1) == int:
        # enter python interpreter here
        do_something()
    else:
        do_something_else()

is there a way to drop into the command line interpreter where i have the comment? so that if i run the following in python:

>>> import my_module
>>> do_stuff(1)

i get my next prompt in the scope and context of where i have the comment in do_stuff()?

like image 384
aaronstacy Avatar asked Jan 28 '10 21:01

aaronstacy


People also ask

How do you go into interactive mode in Python?

On Windows, bring up the command prompt and type "py", or start an interactive Python session by selecting "Python (command line)", "IDLE", or similar program from the task bar / app menu. IDLE is a GUI which includes both an interactive mode and options to edit and run files.

Does interpreter helps in executing the Python program?

The interpreter's job is to take these code objects and follow the instructions. You may be surprised to hear that compiling is a step in executing Python code at all. Python is often called an "interpreted" language like Ruby or Perl, as opposed to a "compiled" language like C or Rust.

How do I select Python interpreter in terminal?

By default, the Python extension looks for and uses the first Python interpreter it finds in the system path. To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P). Note: If the Python extension doesn't find an interpreter, it issues a warning.

How do I run a Python script in interpreter?

The simplest way to start the interpreter is to open the terminal and then use the interpreter from the command line. To open the command-line interpreter: On Windows, the command-line is called the command prompt or MS-DOS console. A quicker way to access it is to go to Start menu → Run and type cmd.


3 Answers

If you want a standard interactive prompt (instead of the debugger, as shown by prestomation), you can do this:

import code
code.interact(local=locals())

See: the code module.

If you have IPython installed, and want an IPython shell instead, you can do this for IPython >= 0.11:

import IPython; IPython.embed()

or for older versions:

from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
ipshell(local_ns=locals())
like image 64
Matt Anderson Avatar answered Oct 05 '22 16:10

Matt Anderson


Inserting

import pdb; pdb.set_trace()

will enter the python debugger at that point

See here: http://docs.python.org/library/pdb.html

like image 42
prestomation Avatar answered Oct 05 '22 18:10

prestomation


If you want a default Python interpreter, you can do

import code
code.interact(local=dict(globals(), **locals()))

This will allow access to both locals and globals.

If you want to drop into an IPython interpreter, the IPShellEmbed solution is outdated. Currently what works is:

from IPython import embed
embed()
like image 37
Ronan Paixão Avatar answered Oct 05 '22 17:10

Ronan Paixão