I have a python file and within that file I want to start an interpreter within that context. Is that possible?
I'm currently starting an interpreter and then using exec to load in the file, but it would be quicker and easier to do it the other way around.
The use case is loading in a file full of data and helper functions that I then want to play around with in the interpreter.
CodePudding user response:
-i flag when running Python interpreter makes you stay in the interpreter after script finishes running.
python -i file.py
CodePudding user response:
The -i flag might be what you are looking for. From man python:
-i When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or
the command. It does not read the $PYTHONSTARTUP file. This can be useful to inspect global variables or a stack trace
when a script raises an exception.
So for example, python -i my_script.py will run my_script.py, then leave you in an interactive session inside the same interpreter once the script completes.
