I try to make game in python using pygame. It should be learning game, so I want to ask if it is possible to run python shell in game window or I have to program my own parser?
-
There are `eval()`, `exec()` functions. `python3 -midlelib` is implemented in Python. – jfs Dec 29 '14 at 08:49
-
@J.F.Sebastian please never recommend eval/exec without mentioning their serious security and performance implications; especially to obivious beginners like OP. Comments like yours are probably a contributor to the countless "I want to program a calculator with exec()" questions... OP, your question is too broad - you can embed all kinds of things into your game; as stated you _can_ use exec/eval but that lets the user excecute arbitrary python code. Depending on what you want to do exactly, you could use the `cmd` module to create an interactive shell, and/or write a small parser for a DSL. – l4mpi Dec 29 '14 at 09:02
-
@l4mpi: OP explicitly asks about "python shell". Not calculator, not coffee machine but about **python** shell. Look at the author of [this answer to "Evaluating a mathematical expression in a string"](http://stackoverflow.com/a/9558001/4279) – jfs Dec 29 '14 at 09:05
-
@J.F.Sebastian and OP does not make clear that "python shell" means the same thing to them it means to you and me. Why would a "learning game" need the user to enter arbitrary python code? It's unclear if a python shell is really needed, and it's probably far from being the best tool for the job. Also, if OP does not know about exec/eval they also don't know about the security and performance implications (e.g. you _can't_ restrict access to builtins), thus my comment still holds - you should probably default to linking to that excellent answer of yours when recommending eval/exec. – l4mpi Dec 29 '14 at 09:12
-
`python3 -midlelib` example makes it clear: what type of "python shell" I'm talking about. btw, I don't see security or performance mentioned in [eval/exec docs](https://docs.python.org/3/library/functions.html#eval). You could [submit a documentation patch](https://docs.python.org/devguide/docquality.html) that discusses them (in a separate section with links from the corresponding functions). – jfs Dec 29 '14 at 10:00
-
related: [Embedding IPython Qt console in a PyQt application](http://stackoverflow.com/q/11513132/4279) – jfs Dec 30 '14 at 07:41
1 Answers
subprocess.Popen('python')
will open a Python interpreter in a new window, similar to typing 'python' on a command line. But I suspect that this is not what you meant by 'in a game window'.
cmd.Cmd
creates "A simple framework for writing line-oriented command interpreters" (that use verb object... syntax). It could be used for writing a text adventure game ('go east', 'open box', 'look room', etc).
code.InteractiveInterpreter
is specifically for Python interpreters. It uses compile
and exec
. Idle's Shell is based on a subclass thereof defined in idlelib/PyShell.py. It simulates the Python interactive interpreter in a tkinter window. There is no need to re-write the parser included in compile
; one can tell compile to stop with the abstract syntax tree that is the output of the parser.

- 18,414
- 3
- 40
- 52