简体   繁体   English

如何从 python 脚本启动 shell

[英]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.我想运行一个脚本,该脚本进行一些设置,然后在该环境中打开一个 shell。 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:使用-i参数运行脚本:

python -i my_script.py

This will execute my_script.py and drop to an interactive shell afterwards.这将执行my_script.py并在之后进入交互式 shell。

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现在它将打开python shell,您可以直接访问测试变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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