Interactive Mode

There are two variants of the interactive REPL in Python. The classic basic interpreter is supported on all platforms with minimal line control capabilities.

On Windows, or Unix-like systems with curses support, a new interactive shell is used by default. This enhanced shell supports color, multiline editing, history browsing, and paste mode. To disable color, see the “Controlling color” documentation. Function keys provide additional functionality: F1 enters the interactive help browser pydoc, F2 allows browsing command-line history without output or prompts, and F3 toggles “paste mode” for easier pasting of larger code blocks.

When using the new interactive shell, exit by typing exit or quit (no parentheses required). If the new interactive shell is not desired, it can be disabled via the PYTHON_BASIC_REPL environment variable.

Error Handling

When an error occurs, the interpreter prints an error message and a stack trace. In interactive mode, it then returns to the primary prompt; when input came from a file, it exits with a nonzero exit status. Exceptions handled by an except clause in a try statement are not considered errors in this context. Some errors are unconditionally fatal and cause an exit with a nonzero exit status, particularly internal inconsistencies and some memory exhaustion cases. All error messages are written to standard error; normal output is written to standard output.

Typing the interrupt character (usually Control-C or Delete) at any prompt cancels the input and returns to the primary prompt. Typing an interrupt while a command is executing raises the KeyboardInterrupt exception, which may be handled by a try statement.

Executable Python Scripts

On BSD-style Unix systems, Python scripts can be made directly executable like shell scripts by adding this line at the beginning:

#!/usr/bin/env python3

This assumes the interpreter is on the user’s PATH. The #! must be the first two characters of the file. On some platforms, this line must end with a Unix-style line ending (\n), not a Windows (\r\n) line ending. Note that the hash character # starts a comment in Python.

Make the script executable using the chmod command:

chmod +x myscript.py

On Windows systems, there is no “executable mode.” The Python installer automatically associates .py files with python.exe so double-clicking a Python file runs it as a script. The extension can also be .pyw, which suppresses the console window.

The Interactive Startup File

For interactive Python use, you can have standard commands execute every time the interpreter starts by setting an environment variable named PYTHONSTARTUP to the path of a file containing your startup commands. This is similar to the .profile feature in Unix shells.

This file is only read in interactive sessions, not when Python reads commands from a script or when /dev/tty is given as the explicit source of commands. It executes in the same namespace as interactive commands, so objects defined or imported can be used without qualification. You can also change the prompts sys.ps1 and sys.ps2 in this file.

To read an additional startup file from the current directory, you can add code like this to your global startup file:

if os.path.isfile('.pythonrc.py'): 
    exec(open('.pythonrc.py').read())

To use the startup file in a script, you must do this explicitly:

import os
filename = os.environ.get('PYTHONSTARTUP')
if filename and os.path.isfile(filename):
    with open(filename) as fobj:
        startup_file = fobj.read()
    exec(startup_file)

Customization Modules

Python provides two hooks for customization: sitecustomize and usercustomize. To find your user site-packages directory, run:

import site
site.getusersitepackages()

This will show something like /home/user/.local/lib/python3.x/site-packages. You can create a file named usercustomize.py in that directory and put anything you want in it. It will affect every invocation of Python unless Python is started with the -s option to disable automatic import.

sitecustomize works the same way but is typically created by a system administrator in the global site-packages directory and is imported before usercustomize. See the documentation of the site module for more details.

Note: A problem with the GNU Readline package may prevent the interrupt character from working as described.

  • Related Posts

    Interactive Input Editing and History Substitution

    Some versions of the Python interpreter support editing of the current input line and history substitution, similar to facilities found in the Korn shell and the GNU Bash shell. This…

    Read more

    What Now?

    Reading this tutorial has probably reinforced your interest in using Python — you should be eager to apply Python to solving your real-world problems. Where should you go to learn…

    Read more

    You Missed

    How Zoom Helps You Stay Safe in Cyberspace

    How Zoom Helps You Stay Safe in Cyberspace

    The Top 10 Webinar Platforms for Businesses in 2025

    The Top 10 Webinar Platforms for Businesses in 2025

    Enhancing Client Service: 5 Zoom Strategies for Professional Services Firms

    Enhancing Client Service: 5 Zoom Strategies for Professional Services Firms

    Understanding Omnichannel Customer Service

    Understanding Omnichannel Customer Service

    Zoom Set to Enhance Customer Experience with New Salesforce Service Cloud Voice Integration

    Zoom Set to Enhance Customer Experience with New Salesforce Service Cloud Voice Integration

    Leadership Strategies for Remote Teams