Skip to content

PYTHONDONTWRITEBYTECODE documentation is incorrect #143134

Description

@mikekaganski

Documentation

PYTHONDONTWRITEBYTECODE is documented in Doc/using/cmdline.rst as:

If this is set to a non-empty string, Python won't try to write .pyc files on the import of source modules.

This is incorrect. The variable is obtained in Python/initconfig.c as:

    int dont_write_bytecode = 0;
    _Py_get_env_flag(use_env, &dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
    if (dont_write_bytecode) {
        config->write_bytecode = 0;
    }

and _Py_get_env_flag reads the environment variable value as integer, treating non-integer string as 1. Specifically, this means that PYTHONDONTWRITEBYTECODE=0 will not suppress writing .pyc files, even though the environment variable is assigned a non-empty string "0".

Linked PRs

Activity

  1. StanFromIreland commented on Dec 24, 2025

    @StanFromIreland
    Member

    The same can be said for several others:

    $ PYTHONDONTWRITEBYTECODE=0 ./python -c "import sys; print(sys.flags.dont_write_bytecode)"
    0
    $ PYTHONINSPECT=0 ./python -c "import sys; print(sys.flags.inspect)"
    0
    >>> exit
    $ PYTHONNOUSERSITE=0 ./python -c "import sys; print(sys.flags.no_user_site)"
    0
    
  2. Javed-Shekh777 commented on Dec 24, 2025

    @Javed-Shekh777

    Hi, I’d like to work on this documentation fix and submit a PR.

  3. serhiy-storchaka commented on Dec 24, 2025

    @serhiy-storchaka
    Member

    The documented behavior is the initial behavior. Since the change is not documented, it was not intentional change.

    It seems that it was changed in a7368ac (bpo-32030). Since it was so long ago, we cannot longer consider it a behavior bug, but a documentation bug. The documentation should be made reflecting the current behavior, and the versionchanged directives should be added.

    cc @vstinner

  4. vstinner commented on Dec 24, 2025

    @vstinner
    Member

    Other environment variables are impacted (they also use _Py_get_env_flag()):

    • PYTHONDEBUG
    • PYTHONDONTWRITEBYTECODE
    • PYTHONINSPECT
    • PYTHONLEGACYWINDOWSSTDIO
    • PYTHONNOUSERSITE
    • PYTHONOPTIMIZE
    • PYTHONUNBUFFERED
    • PYTHONVERBOSE
  5. StanFromIreland commented on Dec 24, 2025

    @StanFromIreland
    Member

    PYTHONINSPECT

    IIUC Serhiy correct (I'm not quite sure why) the behaviour of this one is correct #143135 (comment)

  6. serhiy-storchaka commented on Dec 24, 2025

    @serhiy-storchaka
    Member

    For all of these variables check whether setting the variable to "0" actually changes the behavior or only sys.flags. If it only changes the latter, then this is the bug in the code, otherwise the bug is in the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions