Using the Python Interpreter

Python interpreter

The Python interpreter is usually installed in /usr/local/bin/python on machines where it is available;

Putting /usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command: python to the shell.

When you start Python in interactive mode, it prompts for the next command, usually three greater-than signs (“>>> “).

The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt, eg:

Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> the_world_is_flat = 1
>>> if the_world_is_flat:
... print "Be careful not to fall off!"
...

Python Interactively

When you use Python interactively, it is handy to have some standard commands executed every time the interpreter is started.

You can do this by setting an environment variable named PYTHONSTARTUP to the name of a file containing your start-up commands.

This is similar to the .profile feature of the Unix shells.

To do that, add this line to your .bashrc file:
>> export PYTHONSTARTUP=$HOME/.pythonstartup

Create (or modify) the .pythonstartup file and put your python code in there:

import os
os.system('ls -l')

To exit from the Python interactive prompt, we’ll hit Ctrl+D (in Linux)

See Pythons official documentation for more information.

Leave a Reply

Your email address will not be published. Required fields are marked *