Alexander Soare
Alexander Soare

Reputation: 3247

How to start a shell from a python script

I'd like to run a script which does some setup then opens up a shell with that environment. So rather than then doing

$ python
>>> # do some setup
>>> # start doing what I really came here to do

I want to do

$ python my_script.py
>>> # start doing what I really came here to do

Upvotes: 0

Views: 153

Answers (2)

Klaus D.
Klaus D.

Reputation: 14369

Run your script with the -i argument:

python -i my_script.py

This will execute my_script.py and drop to an interactive shell afterwards.

Upvotes: 3

jaswanth
jaswanth

Reputation: 525

you can do some thing like this

import code

variables = {"test": True}
shell = code.InteractiveConsole(variables)
shell.interact()

now it will open python shell and you can access test variable directly

Upvotes: 2

Related Questions