Running Python Programs

The UNIX Way: Executable Bit, chmod

  • In UNIX, file extensions have no special meaning

  • Python programs generally don’t have a .py extension

  • Rather, programs are executable through their mode

Normal file (default mode 644)
$ ls -l hello-unix
-rw-r--r-- 1 jfasch jfasch 40 Jan 20 09:06 hello-unix
Executable file: mode 755 (executable by everybody)
$ chmod 755 hello-unix
$ ls -l hello-unix
-rwxr-xr-x 1 jfasch jfasch 40 Jan 20 09:07 hello-unix

The UNIX Way: Hash-Bang (She-Bang)

  • Scripts (Shell, Python, Perl, AWK, …) need an interpreter though

  • Interpreter line, Hash-Bang, She-Bang: first line in a script, usually

    #!/usr/bin/python
    
    ... here goes Python code ...
    
  • Sometimes python is Python 2 (on the Raspberry, for example)

    #!/usr/bin/python3
    ...
    
  • In Virtual Environments things are different

    • Python interpreter is not /usr/bin/python

    • python is taken from PATH setting of the environment

    #!/usr/bin/env python
    ...
    

The UNIX Way: Running

  • Prerequisites

    • Script is executable

    • Scripts has She-bang

#!/usr/bin/python

print('Hello World')
  • UNIX uses $PATH environment variable to find programs

  • Current working directory is not usually in $PATH

  • ⟶ specify . explicitly

$ ./hello-unix
Hello World

The Doze Way: Registry

  • In Doze, file extensions have special meaning

  • In Doze, there is no executable bit, no mode

  • .py files are executed by the Python interpreter

    • Extension not necessary when invoking

  • How does the system know?

    • During Python installation, the installer registers the Python program as being responsible for .py

    • Somewhere in the Windows registry.

  • Doze always looks in the current working directory [1]

print('Hello World')
> hello-doze
Hello World

Footnotes