Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed (create) an interactive Python shell inside a Python program

Tags:

python

Is it possible to start an interactive Python shell inside a Python program?

I want to use such an interactive Python shell (which is running inside my program's execution) to inspect some program-internal variables.

like image 737
zJay Avatar asked Apr 08 '11 16:04

zJay


People also ask

What is interactive Python shell in Python?

The Python interactive console (also called the Python interpreter or Python shell) provides programmers with a quick way to execute commands and try out or test code without creating a file.

Where is Python interactive shell?

To start the Python language shell (the interactive shell), first open a terminal or command prompt. Then type the command python and press enter. Type "help", "copyright", "credits" or "license" for more information. After the symbols >>> you can type a line of Python code, or a command.

Can Python run in interactive mode?

The interactive Python mode lets you run your script instantly via the command line without using any code editor or IDE. To run a Python script interactively, open up your command line and type python. Then hit Enter. You can then go ahead and write any Python code within the interactive mode.


1 Answers

The code module provides an interactive console:

import readline # optional, will allow Up/Down/History in the console import code variables = globals().copy() variables.update(locals()) shell = code.InteractiveConsole(variables) shell.interact() 
like image 73
phihag Avatar answered Oct 03 '22 06:10

phihag