简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM