{"id":5072,"library":"subprocess32","title":"subprocess32 Backport for Python 2","description":"subprocess32 is a backport of the `subprocess` standard library module from Python 3 (specifically APIs from versions 3.2 through 3.5) for use on Python 2.x. It includes critical bugfixes, notably for reliability in threaded applications on POSIX systems, and new features like timeout support (from Python 3.3) and the `run()` API (from Python 3.5). The latest version is 3.5.4, released in May 2019. The project is explicitly end-of-life (EOL) as Python 2 itself has reached EOL, meaning there are no further active developments or planned releases.","status":"abandoned","version":"3.5.4","language":"en","source_language":"en","source_url":"https://github.com/google/python-subprocess32","tags":["python2","backport","subprocess","posix","eol"],"install":[{"cmd":"pip install subprocess32","lang":"bash","label":"Install on Python 2.x"}],"dependencies":[],"imports":[{"note":"On Python 2, directly importing `subprocess` (the built-in module) can lead to thread-safety issues and lacks modern features. The recommended pattern is to conditionally import `subprocess32` to leverage its fixes and features on Python 2, falling back to the built-in `subprocess` if `subprocess32` isn't available, and using the standard `subprocess` on Python 3.","wrong":"import subprocess","symbol":"subprocess","correct":"import os, sys\n\nif sys.version_info[0] < 3:\n    try:\n        import subprocess32 as subprocess\n    except ImportError:\n        import subprocess\nelse:\n    import subprocess"}],"quickstart":{"code":"import os, sys\n\n# Recommended import pattern for cross-version compatibility\nif sys.version_info[0] < 3:\n    try:\n        import subprocess32 as subprocess\n    except ImportError:\n        print(\"Warning: subprocess32 not found, using native subprocess module.\")\n        import subprocess\nelse:\n    import subprocess\n\ntry:\n    # Execute a simple command\n    result = subprocess.run(\n        ['echo', 'Hello, subprocess32!'],\n        capture_output=True,\n        text=True, # In Python 2, this maps to universal_newlines=True\n        check=True,\n        timeout=5\n    )\n    print(\"STDOUT:\", result.stdout.strip())\n    print(\"STDERR:\", result.stderr.strip())\n\n    # Example with error\n    subprocess.run(['false'], check=True)\nexcept subprocess.CalledProcessError as e:\n    print(f\"Command failed with exit code {e.returncode}\")\nexcept subprocess.TimeoutExpired:\n    print(\"Command timed out\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\n","lang":"python","description":"This quickstart demonstrates the recommended import pattern and basic usage of the `subprocess.run()` function, which was backported from Python 3.5. It includes capturing output, handling errors with `check=True`, and setting a timeout. Note that `text=True` is an alias for `universal_newlines=True` within `subprocess32` for Python 2."},"warnings":[{"fix":"Migrate applications from Python 2 to Python 3. Python 3's built-in `subprocess` module provides the same or superior functionality and thread safety.","message":"The `subprocess32` library is explicitly end-of-life (EOL) along with Python 2. This means no further development, bug fixes, or security updates are planned. Relying on it in new projects is strongly discouraged.","severity":"breaking","affected_versions":"All versions (project-wide)"},{"fix":"Avoid using `subprocess32` on Windows. For cross-platform Python 2 code, consider a conditional import that uses the native `subprocess` module on Windows, or use alternative process management libraries that explicitly support Windows.","message":"`subprocess32` is primarily designed for POSIX systems (Linux, macOS) and has not been tested or fully supported on Windows or other non-POSIX platforms. Attempts to install or use it on Windows may lead to compilation errors (e.g., missing `unistd.h`) or unexpected behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always consult Python 3.2 documentation for `subprocess` when using `subprocess32`, and explicitly check the `subprocess32` GitHub README for details on backported features like `run()` and `timeout`. Do not assume full parity with newer Python 3 versions.","message":"While `subprocess32` backports `timeout` support (from Python 3.3) and the `run()` API (from Python 3.5), other features and APIs are 'frozen at the 3.2 level'. This means `subprocess32` is not a complete, feature-for-feature replica of the latest Python 3 `subprocess` module.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use `subprocess32` (via the conditional import pattern) when running Python 2.x code that spawns subprocesses, especially in threaded environments. For new development, migrate to Python 3.","message":"The primary motivation for `subprocess32` was to address race conditions and improve thread-safety in Python 2.x's native `subprocess` module on POSIX systems, particularly in multi-threaded applications. Not using `subprocess32` (i.e., using the built-in `subprocess` on Python 2) can lead to unreliable behavior and crashes when processes are spawned in threaded contexts.","severity":"gotcha","affected_versions":"Python 2.x (native subprocess module)"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}