Is there a way to execute a Python script, yet stay in the Python shell thereafter, so that variable values could be inspected and such?
Asked
Active
Viewed 315 times
1
-
I am not 100% sure if this is what you want, but you could try using the python debugger [pdb](https://docs.python.org/3/library/pdb.html) – Carlos Gonzalez Aug 14 '17 at 14:22
-
The IDLE which ships with Python (you still have to download it on Linux) [has a built-in debugger](https://inventwithpython.com/chapter7.html). – Christian Dean Aug 14 '17 at 14:22
-
If you just want to run a Python script and have a REPL afterwards, IDLE is really pretty nice. Just open the file, run it, and then you have the REPL already there. – poke Aug 14 '17 at 14:25
-
Does `execfile` fit what you need? https://stackoverflow.com/questions/5280178/how-do-i-load-a-file-into-the-python-console – Kyle Burton Aug 14 '17 at 14:26
-
You can try [pudb](https://pypi.python.org/pypi/pudb), I like to use this. – Akhilesh Aug 14 '17 at 14:28
-
You can [jupyter notebook](http://jupyter.org/) as well. – Akhilesh Aug 14 '17 at 14:30
-
2I think you are looking for `python -i ./file.py`, where the `-i` flag will enter interactive mode after executing the file. If you are already in the console, then `execfile`. – Metaphox Aug 14 '17 at 14:46
2 Answers
1
Metaphox nailed it:
I think you are looking for python -i ./file.py, where the -i flag will enter interactive mode after executing the file. If you are already in the console, then execfile. – Metaphox 2 mins ago
But I want to thank for the other suggestions as well, which go beyond the original question yet are useful!

Happysmithers
- 733
- 2
- 8
- 13
1
For those who use IPython you can use the embed()
function for some extra flexibility since it allows you to drop into a shell anywhere in your program:
from IPython import embed
def some_function(args):
# ... do stuff ...
embed() # drop to shell here
# ... back to the function ...

Hannes Ovrén
- 21,229
- 9
- 65
- 75