如何启动 Python ipython shell,在其中自动运行一些命令,并使其保持打开状态?
我尝试
echo "print 'hello'" | ipython
运行该命令,但 ipython 之后立即退出。
有什么想法吗?谢谢!
编辑: 我实际上需要将命令传递到交互式 Django shell 中,例如:
echo "print 'hello'" | python manage.py shell
所以 gimel 建议的 -i 开关似乎不起作用(执行后 shell 仍然退出)
I tried
echo "print 'hello'" | ipython
Which runs the command but ipython immediately exits afterwards.
Any ideas? Thanks!
Edit:
I actually need to pass the command into the interactive Django shell, e.g.:
echo "print 'hello'" | python manage.py shell
so the -i switch gimel suggested doesn't seem to work (the shell still exits after execution)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用标准解释器使用的相同标志
-i
。Linux 示例,使用
- c
命令行标志:Use the same flag used by the standard interpreter,
-i
.A Linux example, using the
-c
command line flag:尝试在 ~/.ipython 中使用 ipy_user_conf.py
Try using the ipy_user_conf.py inside your ~/.ipython
我不确定 ipython,但基本的 python 解释器有一个命令行参数,可以在执行您给它的文件后给您提示。我没有方便的解释器来告诉您它是什么,但您可以使用 python --help 来获取它。它应该完全符合您的要求。
I'm not sure of ipython but the basic python interpreter has a command line parameter to give you the prompt after it executes the file you've given it. I don't have an interpreter handy to tell you what it is but you can get it using
python --help
. It should do exactly what you want.使用 Django shell 运行自定义启动脚本/配置文件脚本被标记为已关闭:不会修复。
然而,该票证中讨论了一个 shell_plus Django 扩展,它似乎可以满足您的要求。我还没有机会检查它,但看起来至少它可以运行一个负载来自动导入所有已安装应用程序的所有模型(我经常这样做。发现自己正在这样做)。
Running a custom startup script/profile script with the Django shell was marked as closed: wontfix.
However, there is a shell_plus Django extension discussed in that ticket which seems to do what you want. I haven't had a chance to check it out, but it looks like at the very least it can run a load to auto import all the models of all installed apps (which I usu. find myself doing).