-5

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?

6
  • There are eval(), exec() functions. python3 -midlelib is implemented in Python. Commented Dec 29, 2014 at 8: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. Commented Dec 29, 2014 at 9: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" Commented Dec 29, 2014 at 9: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. Commented Dec 29, 2014 at 9: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. You could submit a documentation patch that discusses them (in a separate section with links from the corresponding functions). Commented Dec 29, 2014 at 10:00

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.