elprup
elprup

Reputation: 1998

Can python shell have some pre-input?

When testing in python shell, I always have to type some import like:

Python 2.5.4 (r254:67916, Jun 24 2010, 15:23:27) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>import sys
>>>import datetime

Can someone help me to automatically finish these? It means I run some command to enter python shell it has already done import for me, and a python shell waiting for me to continue type command.

Thanks.

Upvotes: 0

Views: 317

Answers (2)

Jim Brissom
Jim Brissom

Reputation: 32969

Create a file with the commands you want to execute during startup, and set the environment variable PYTHONSTARTUP to the location of that file. The interactive interpreter will then load and execute that file. See http://docs.python.org/tutorial/interpreter.html#the-interactive-startup-file

On a sidenote, you might want to consider ipython as an improved Python shell when working in interactive mode.

Upvotes: 1

dkamins
dkamins

Reputation: 21918

Try:

python -i -c "import sys; import datetime;"

More info:

-i     : inspect interactively after running script; forces a prompt even
         if stdin does not appear to be a terminal; also PYTHONINSPECT=x

&

-c cmd : program passed in as string (terminates option list)

Upvotes: 3

Related Questions