GJ.
GJ.

Reputation: 5364

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!

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)

Upvotes: 3

Views: 4816

Answers (4)

GJ.
GJ.

Reputation: 5364

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

Upvotes: 2

David
David

Reputation: 681

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

Upvotes: 0

gimel
gimel

Reputation: 86482

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]:

Upvotes: 5

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72815

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.

Upvotes: 0

Related Questions