Running Python Programs under Unix



Assumptions and notes:

There are several ways to execute a python program:

  1. Interactively:
    rucs @ /home/nokie > python
    Python 1.5.1 (#1, Nov 29 1999, 14:33:24)  [GCC 2.8.1] on sunos5
    Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
    >>> i = 5
    >>> print i    
    5
    >>> ^D
    rucs @ /home/nokie > 
    
    
  2. Run the interpreter with the file as command line argument:
    rucs @ /home/nokie > python somefile
    3
    rucs @ /home/nokie > 
    
  3. Run the interpreter and execute the file:
    rucs @ /home/nokie> python
    Python 1.5.1 (#1, Nov 29 1999, 14:33:24)  [GCC 2.8.1] on sunos5
    Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
    >>> execfile("somefile")        
    3
    
  4. Interpret the file directly and go into interpreter (good for debugging):
    rucs @ /home/nokie > python -i somefile
    3
    >>> print i+1
    4
    >>> 
    
  5. As a script: Put the line:
     #!/usr/bin/python 
    #!/usr/local/bin/python in as the first line of file afile (starting in column 1), make afile executable, and run afile as a script:
    rucs @ /home/nokie > cat afile
    #!/usr/local/bin/python
    
    #!/usr/local/bin/python
    
    print "This is a script"
    rucs @ /home/nokie > chmod 700 afile 
    rucs @ /home/nokie > afile
    This is a script
    rucs @ /home/nokie > 
    
    To find the current location of python, enter
    which python
    


Last modified: