Python is formally an interpreted language,  you can look at the  discussion on interpreted and compiled languages here. Python programs are executed by a software known as the Python Interpreter. 

We can use the interpreter interactively especially while debugging, but in most cases , the commands are defined and saved in a text file known as the source code or python script.   A Python source code is conventionally saved with a .py  extension/suffix for example foo.py, this is just a convention you can use a file with any  valid extension  such as  foo.txt  and the interpreter will happily execute your program. However,  in reality you will almost always want to use .py extension for Python scripts, there are many reasons why you should stick to this convention, some of these reasons are outlined below:

  • The Operating system will recognize the file as a Python file and not just any other  file and will offer it some special support. 
  • Text Editors/ IDEs will treat a file with a .py extension as a Python file by default. This will allow you to access all the feature offered by the editor/IDE for Python programming such as code highlighting, auto-completion, indentation, debugging and many others without extra setup. 
  • The Python import system will only work for Python files with a .py extension you will not be able to import a file with some other extensions.
  • Users will easily associate a .py  extension with a Python file even without seeing it's content.

How does the Python interpreter Work?

The Interpreter performs several operations on the source code,

  • Analyzing the source code

    The interpreter starts by analyzing/parsing the source code to check that it is actually a valid Python program by ensuring that it is correctly  formatted. It performs lexical analysis on the source code to ensure that Python syntax is strictly adhered to, otherwise it raises  syntax error before terminating the process. It also checks to ensure that indentation rules are followed , it raises Indentation error if not.

  • Generating Bytecode

    The High level  Python program  would be useless  if there was no way to turn it into instructions that a computer will understand. Bytecode  are set of intermidiate instructions that are executable by the Python Virtual Machine. The Python interpreter, at this phase,  turns the source code into bytecode . The bytecode are saved in a file with a .pyc extension in the same directory as the .py  file  and are executable from any computer which have the PVM(python virtual machine) . Any error encountered while  the translation  is on-going is known as  a runtime error.

  • Initializing the Python Virtual Machine

    The Python virtual machine(PVM) is responsible for  translating the bytecode into the actual machine code, and displaying the final output. The virtual machine  does all the necessary architecture adjustments, making the Python program highly portable. It executes the bytecode one statement at a time, reporting the output of that particular execution.

So the above process can be summarized as:

 Processes in Python file execution