Skip to main content

Overview

The Very High Level Layer provides simple functions to execute Python code from C without detailed interaction with the interpreter. These functions accept Python source code as strings or files.
These functions use FILE* parameters. Ensure the FILE structures come from the same C runtime library as Python to avoid compatibility issues.

Running Python Code

From Strings

PyRun_SimpleString

Execute Python code from a string in the __main__ module.
const char*
required
Python source code to execute
Returns: 0 on success, -1 if an exception was raised Example:
Unhandled SystemExit exceptions will exit the process (unless PyConfig.inspect is set).

From Files

PyRun_SimpleFile

Execute Python code from a file.
FILE*
required
Open file pointer to read Python code from
const char*
required
Filename for error messages (decoded from filesystem encoding)
Returns: 0 on success, -1 on error Example:
On Windows, open files in binary mode ("rb") to handle line endings correctly.

Interactive Execution

PyRun_InteractiveOne

Read and execute a single statement from an interactive device.
FILE*
required
Interactive input source (typically stdin)
const char*
required
Filename for prompts and error messages
Returns:
  • 0 - Input executed successfully
  • -1 - An exception occurred
  • Error code from errcode.h - Parse error
The user is prompted with sys.ps1 and sys.ps2.

PyRun_InteractiveLoop

Read and execute statements until EOF, creating a Python REPL. Returns: 0 at EOF, negative on failure Example:

Advanced Execution

PyRun_String

Execute Python code with specific namespace.
const char*
required
Python source code to compile and execute
int
required
Grammar start symbol (see Start Symbols below)
PyObject*
required
Global namespace dictionary
PyObject*
required
Local namespace (can be any mapping object)
Returns: Result of execution as PyObject*, or NULL on error Example:

Compilation

Py_CompileString

Compile Python source to a code object without executing.
const char*
required
Python source code
const char*
required
Filename for error messages and tracebacks
int
required
Start symbol: Py_file_input, Py_eval_input, or Py_single_input
Returns: Code object, or NULL on error Example:

PyEval_EvalCode

Execute a precompiled code object.
PyObject*
required
Code object from Py_CompileString
PyObject*
required
Global namespace dictionary
PyObject*
required
Local namespace dictionary

Start Symbols

Start symbols determine what kind of Python code can be compiled:
int
Single expression - returns the expression value
int
Sequence of statements - typical for modules/files
int
Single statement - used in interactive interpreters
int
Function type annotation - requires PyCF_ONLY_AST flagUsed for parsing PEP 484 signature type comments.

Compiler Flags

Control compilation behavior with PyCompilerFlags:

Common Flags

  • PyCF_ALLOW_TOP_LEVEL_AWAIT - Allow await at module level
  • PyCF_ONLY_AST - Return AST instead of code object
  • PyCF_TYPE_COMMENTS - Enable PEP 484 type comments
Example:

Input Hooks

Customize interactive input behavior:

PyOS_InputHook

Function called when the interpreter becomes idle waiting for input. Useful for integrating with GUI event loops. Example:

PyOS_ReadlineFunctionPointer

Override line reading function for interactive input. Example:

See Also

Introduction

C API basics and setup

Import Modules

Import and use Python modules

Exception Handling

Handle execution errors

Utilities

Argument parsing and utilities