Skip to main content

Python/initconfig.c

cpython 3.14 @ ab2d84fe1023/Python/initconfig.c

Python/initconfig.c owns interpreter startup configuration. It parses environment variables (PYTHONPATH, PYTHONDONTWRITEBYTECODE, etc.), command-line flags (-O, -W, -X), and the computed paths (sys.path, sys.prefix) into a PyConfig struct, then passes that struct to Py_InitializeFromConfig.

Map

LinesSymbolRole
1-300PyConfig defaults, PyConfig_InitPythonConfigZero config struct with sane defaults
301-700Environment variable parsingPYTHONPATH, PYTHONINSPECT, PYTHONOPTIMIZE, etc.
701-1200Command-line parsing-c, -m, -W, -X flags
1201-1800_PyConfig_InitPathConfigsys.prefix, sys.exec_prefix, sys.path computation
1801-3200Py_InitializeFromConfigDrive full interpreter initialization sequence

Reading

PyConfig struct (selected fields)

// CPython: Include/cpython/initconfig.h (used by initconfig.c)
typedef struct {
int isolated;
int use_environment;
int dev_mode;
int optimization_level; /* -O flag count */
int verbose;
wchar_t *program_name;
PyWideStringList argv;
PyWideStringList xoptions; /* -X options */
PyWideStringList warnoptions;/* -W options */
wchar_t *pythonpath_env; /* PYTHONPATH */
int write_bytecode;
...
} PyConfig;

sys.path computation

_PyConfig_InitPathConfig calls _PyPathConfig_ComputeSysPath0 to find the script directory and _PyGetPath to compute the initial sys.path from PYTHONPATH, the installation prefix, and zipimport paths. This is the most platform-dependent part of initialization.

-X options

-X dev enables development mode (extra assertions, tracemalloc). -X faulthandler installs the fault handler. -X importtime measures import times. These are parsed from config.xoptions and applied during Py_InitializeFromConfig.

Py_InitializeFromConfig sequence

  1. Apply PyConfig settings to the interpreter state.
  2. Initialize types (_PyTypes_Init).
  3. Initialize builtins (_PyBuiltins_Init).
  4. Initialize sys module.
  5. Initialize importlib and frozen modules.
  6. Set sys.path.

gopy notes

gopy initializes via pythonrun/runstring.go and the stdlibinit registry. There is no PyConfig struct; configuration is passed as Go arguments to the run function. sys.path is a static list of built-in module names since path-based import is not yet implemented.