简体   繁体   English

如何启动Python ipython shell,在其中自动运行一些命令,并保持打开状态?

[英]How to start a Python ipython shell, automatically run inside it a few commands, and leave it open?

I tried 我试过了

echo "print 'hello'" | ipython

Which runs the command but ipython immediately exits afterwards. 运行命令,但是ipython之后立即退出。

Any ideas? 有任何想法吗? Thanks! 谢谢!

Edit: I actually need to pass the command into the interactive Django shell, eg: 编辑:实际上,我需要将命令传递到交互式Django shell中,例如:

echo "print 'hello'" | python manage.py shell

so the -i switch gimel suggested doesn't seem to work (the shell still exits after execution) 因此建议的-i开关gimel似乎不起作用(执行后Shell仍退出)

Use the same flag used by the standard interpreter, -i . 使用与标准解释器-i相同的标志。

-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, even when sys.stdin does not appear to be a terminal. 当将脚本作为第一个参数传递或使用-c选项时,即使sys.stdin似乎不是终端,在执行脚本或命令后也要进入交互模式。 The PYTHONSTARTUP file is not read. 无法读取PYTHONSTARTUP文件。

A Linux example, using the -c command line flag : 一个Linux示例,使用-c命令行标志

$ ipython -i -c 'print "hello, ipython!"'
hello, ipython!

In [2]: print "right here"
right here

In [3]:

尝试在〜/ .ipython中使用ipy_user_conf.py

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. 我不确定ipython,但基本的python解释器具有命令行参数,在执行给定的文件后会提示您。 I don't have an interpreter handy to tell you what it is but you can get it using python --help . 我没有方便的解释器来告诉您它是什么,但是您可以使用python --help来获得它。 It should do exactly what you want. 它应该完全按照您想要的去做。

Running a custom startup script/profile script with the Django shell was marked as closed: wontfix . 使用Django Shell运行自定义的启动脚本/配置文件脚本被标记为已关闭:wontfix

However, there is a shell_plus Django extension discussed in that ticket which seems to do what you want. 但是,该票证中讨论了shell_plus Django扩展,它似乎可以满足您的要求。 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). 我还没有机会进行检查,但看起来至少它可以运行负载以自动导入所有已安装应用程序的所有模型(我通常会这样做)。

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

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