Beginner   Chapter 7 · 25 min read · Module 1: Python Fundamentals

Python Interpreter and IDE

Learning objectives
  • After this chapter you can explain what the Python interpreter is and how it runs your code.
  • You can tell the difference between interactive mode and script mode and use both.
  • You can describe what bytecode is and why a .pyc file may appear.
  • You can name popular editors and IDEs and say what features they give you.
  • You can run a Python file from the command line and read a simple error message.

When you write Python code, something has to read it and make the computer do the work. That something is the Python interpreter. It is a program that takes your text, checks it, and runs it one step at a time. Without the interpreter, a Python file is only plain text.

You can talk to the interpreter in two main ways. You can type one line at a time at a prompt and see the answer at once, or you can save many lines in a file and ask the interpreter to run the whole file. Both ways are useful, and good programmers switch between them all day.

Writing code in a basic text editor works, but most people prefer a tool that colours the code, finds mistakes and runs the program with one click. Such tools are called editors and IDEs. In this chapter you will learn how the interpreter works, how to use it in different ways, and how to pick a tool that suits you.

What is the Python interpreter?

A computer processor understands only very low-level instructions made of numbers. Humans do not like writing those, so we use high-level languages such as Python. Something must translate our readable code into actions. For Python, that translator is the interpreter. The standard one is called CPython because it is written in the C language. When people say "install Python", they usually mean installing CPython, which gives you the python or python3 command.

Interpreter: a program that reads your source code and carries it out, without making you build a separate machine-code file first

Other interpreters exist too. PyPy is a faster one that uses a just-in-time compiler. Jython runs on the Java platform and MicroPython runs on tiny devices. They all aim to run the same Python language, but CPython is the reference version and the one this course uses.

What happens when you run a program

Many people say Python is "interpreted" and not "compiled", but the real story has two steps. First, CPython compiles your source code into a compact form called bytecode. Bytecode is a list of simple instructions that is not tied to one kind of computer. Second, a part of the interpreter called the Python Virtual Machine (PVM) reads the bytecode and executes the instructions one by one.

  1. The interpreter reads your source text and checks the syntax. If you wrote something invalid, it stops here with a SyntaxError.
  2. The source is compiled into bytecode in memory.
  3. The virtual machine runs the bytecode line by line.
  4. If a problem appears while running, such as dividing by zero, the program stops and shows a traceback.

When you import a module, Python may save its bytecode in a folder named __pycache__ as a .pyc file. This is only a speed-up so the next import does not need to compile again. You can delete these files safely and Python will make them again.

Example 1: Compile and run a small piece of source
source = "total = 2 + 3\nprint('Total is', total)"
code_object = compile(source, '<demo>', 'exec')
print(type(code_object).__name__)
exec(code_object)
code Total is 5

This example does by hand what the interpreter does for every program. The compile function turns text into a code object, which holds bytecode. The exec function then runs it. You will rarely need these two in daily work, but they show that running code is really a compile step followed by an execute step.

Interactive mode (the REPL)

If you type python3 in a terminal with no file name, you enter interactive mode. You see a prompt that looks like >>>. You type a statement, press Enter, and Python runs it immediately. This is called a REPL, which stands for Read, Evaluate, Print, Loop. It is perfect for testing a small idea, trying a function, or using Python as a calculator.

Example 2: A short interactive session
>>> 7 * 6
42
>>> name = "Asha"
>>> name.upper()
'ASHA'
>>> len(name)
4
>>> exit()
42 'ASHA' 4

Notice that in the REPL you do not need print to see the value of an expression. The REPL shows the result for you. In a script file this does not happen, so you must call print there. A line starting with ... appears when you type a block such as an if or a for, because the REPL waits for the rest of the block.

Press Ctrl+D (Linux and macOS) or Ctrl+Z then Enter (Windows) to leave the REPL, or type exit(). The REPL keeps your variables only until you close it.

Script mode

For anything longer than a few lines, save your code in a file that ends with .py, called a script. Then run it from a terminal with python3 filename.py. The interpreter reads the whole file from top to bottom and runs it. A script can be run again and again, shared with friends and kept in version control, which a REPL session cannot do.

Every Python file also has a built-in variable called __name__. When you run a file directly, Python sets __name__ to the text "__main__". When the file is imported by another file, __name__ is the module name instead. This lets one file act as both a program and a library.

Example 3: Script that checks how it was started
def main():
    print('Running as the main program')

print(__name__)

if __name__ == '__main__':
    main()
__main__ Running as the main program

This pattern is very common in real projects. The code under the if runs only when the file is started directly and not when another file imports it.

Example 4: A script that reads input
name = input('Your name: ')
print('Hello,', name)
Your name: Hello, Asha

Here input waits for the user to type a line. In the example, the text Asha is supplied automatically, so the program runs without a keyboard.

Editors and IDEs

A text editor lets you write and save files. A code editor adds features such as colours for keywords and automatic indentation. An IDE (Integrated Development Environment) goes further and bundles the editor, a way to run code, a debugger, a file browser and tools for testing and version control in one window.

ToolTypeGood for
IDLESimple IDE that comes with PythonFirst steps and quick tests
Visual Studio CodeLight editor with extensionsEveryday coding in many languages
PyCharmFull Python IDELarge projects and strong code help
Jupyter NotebookBrowser notebookData work and step-by-step explanations
ThonnyBeginner IDELearning and watching variables change
Vim or NanoTerminal editorsQuick edits on a server

Useful IDE features include syntax highlighting (colours for code parts), auto-complete (suggestions as you type), a debugger (run step by step and inspect values), a built-in terminal and linting (warnings about likely mistakes). None of these are required to learn Python, but they save time and help you notice errors early.

Choosing a tool as a beginner

Do not spend days choosing. IDLE is already installed and is enough for the first chapters. When you want more comfort, Visual Studio Code with the Python extension is a popular free choice, and Thonny is very friendly for learners. Whatever you pick, learn a few shortcuts for running a file and for opening the terminal. Also make sure the editor uses the same Python version that you installed, because many tools can hold several versions at once.

Reading an error from the interpreter

When the interpreter finds a problem it prints a traceback. The last line names the error type and a message, and the lines above show the file name and line number where it happened. Read the last line first, then go to the line it points to. Most beginner errors are typing mistakes, such as a missing colon or a wrong name, and the message usually points straight at them.

Example 5: Running from the command line
$ python3 hello.py
Hello, world!
$ python3 --version
Python 3.12.1
Hello, world! Python 3.12.1

Python syntax

Syntax
python3 script_name.py
Syntax
python3 -c "print('one line of code')"
Syntax
python3 --version
Syntax
if __name__ == '__main__':
    main()

Real-world applications

  • Trying out small ideas quickly in the REPL before putting them in a program.
  • Running automation scripts on servers from the command line.
  • Using an IDE with a debugger to find bugs in large projects.
  • Teaching and data analysis with notebooks that mix code and notes.
  • Using a faster interpreter such as PyPy for heavy calculations.

Common mistakes

Mistake: Typing python3 hello.py inside the REPL instead of the terminal.
Fix: Leave the REPL with exit() first. The command line is outside Python; the >>> prompt is inside it.
Mistake: Expecting a script to show expression values the way the REPL does.
Fix: In a script, use print() to display a value. Only the REPL shows results automatically.
Mistake: Saving a file with a name like random.py or math.py.
Fix: Do not use names of standard modules for your files. Python may import your file instead of the real module and cause strange errors.
Mistake: Running the file with a different Python version than the one the editor shows.
Fix: Check the version with python3 --version and select the right interpreter in your IDE settings.
Mistake: Forgetting the .py ending, or saving the file as .txt.
Fix: Name scripts with the .py ending and make sure the editor does not add a hidden .txt.

Important tips

  • Use the REPL to test a single line before you add it to your script.
  • Learn the keyboard shortcut that runs the current file in your editor.
  • Type help(name) in the REPL to read built-in documentation.
  • Keep each project in its own folder so files stay organised.
  • Read the last line of a traceback first.

Key points summary

  • The interpreter compiles your code to bytecode and then runs it on the Python Virtual Machine.
  • CPython is the standard interpreter.
  • Interactive mode runs one statement at a time; script mode runs a whole file.
  • __name__ is '__main__' when a file is run directly.
  • An IDE bundles an editor, runner, debugger and more; it is a convenience, not a requirement.

Exam and interview points

  • Name the standard interpreter: CPython, written in C.
  • State the steps: source, bytecode, virtual machine.
  • Explain REPL as Read, Evaluate, Print, Loop.
  • Say what __name__ == '__main__' checks.
  • Give two IDE features such as debugger and auto-complete.

Quick revision

  • Interpreter = program that runs Python code.
  • Source is compiled to bytecode, then run by the PVM.
  • Interactive mode uses the >>> prompt.
  • Scripts end in .py and run with python3 file.py.
  • IDE = editor + runner + debugger and more.

Frequently asked questions

Is Python compiled or interpreted?

Both, in a way. CPython first compiles the source to bytecode and then interprets that bytecode on a virtual machine. You never need to run the compile step yourself.

Which IDE is best for beginners?

IDLE and Thonny are the simplest to start with, and Visual Studio Code is a good next step. The best one is the one you feel comfortable using.

What is a .pyc file?

It is cached bytecode that Python saves in the __pycache__ folder to make imports faster. It is safe to delete and is made again when needed.

Do I need an IDE to write Python?

No. A plain text editor and a terminal are enough. An IDE only makes work faster and more pleasant.

What is the difference between python and python3 commands?

On some systems python may point to an old version or not exist, while python3 always means Python 3. On Windows the launcher py often does the same job.

Practise this chapter

22 practice questions Take the chapter quiz

Related chapters