开发者

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.

Any ideas? Thanks!

开发者_Python百科

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)


Use the same flag used by the standard interpreter, -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. The PYTHONSTARTUP file is not read.

A Linux example, using the -c command line flag:

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

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

In [3]:


Try using the ipy_user_conf.py inside your ~/.ipython


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.


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).

  • Shell plus.py in django-command-extensions on Google Code
  • django-command-extensions homepage on Google Code
  • django_extensions on Github
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜