Python compatibility issues come from three sources: differences between Python versions, changes in third-party libraries, and differences between operating systems. You handle them by declaring the versions you support, testing against each of them in continuous integration, pinning dependencies, and using feature checks and small compatibility shims instead of version guessing.
Python 2 reached end of life on 1 January 2020 and is not supported, so new projects should target Python 3 only. Today the real work is supporting several Python 3 minor versions at once, such as 3.10 to 3.13.
Put the supported range in pyproject.toml so installers can refuse an incompatible interpreter.
Each Python minor version receives bug fixes for a limited time and security fixes until its end of life, roughly five years after release. Check the official Python release schedule and drop versions that are past end of life, since keeping them blocks you from newer language features.
You need several interpreters installed to test a library. Common tools are pyenv, uv and the system package manager.
Create a separate virtual environment for each project and interpreter so versions never leak between projects.
Prefer feature detection to version checks. Try the import you need and fall back when it is missing.
When you must branch on the version, compare sys.version_info as a tuple. Type checkers understand this check.
Declare the matching conditional dependency so the backport is installed only where needed.
from __future__ import annotations lets annotations be stored as strings, which helps with forward references on older versions, though the behaviour of annotations keeps evolving in newer releases, so test on every supported version.
Each release removes features that were deprecated earlier. Run your tests with deprecation warnings turned into errors to find problems before an upgrade.
Or configure it in pyproject.toml.
Examples of changes that have broken real code include the removal of the distutils module in Python 3.12 (use setuptools or build tools instead), the removal of asyncore and imp in 3.12, and changes to the datetime.utcnow() function, which is deprecated in 3.12 in favour of datetime.now(timezone.utc). Read the "What's New" page for each release you adopt.
Library incompatibilities are as common as interpreter ones. Use these habits.
requests>=2.31,<3, and exact pinned versions in applications through a lock file.pip check to detect conflicting requirements.A compatibility claim is only true if you test it. Use a matrix in continuous integration, or tox or nox locally.
Run nox to execute the tests under every listed interpreter. Also test on the operating systems you support, because path handling, line endings and available system libraries differ between Linux, macOS and Windows. Use pathlib and os.path instead of hard-coded separators.
No. Python 2 has been unsupported since 1 January 2020, and libraries such as six exist only to bridge it. Use Python 3 and remove six from old code during upgrades.
Look at the package metadata on PyPI, its Requires-Python field and classifiers, or run pip install and read the error if the interpreter is not supported.
sys.version_info and platform.python_version()?sys.version_info is a tuple you can compare numerically, so use it in code. platform.python_version() returns a string, which is better for display or logs.
The usual causes are a different Python version, unpinned dependencies, or an operating system difference. Match the CI interpreter locally and install from the lock file.
Handle Python compatibility by declaring requires-python, testing every supported version and operating system in CI, preferring feature detection and conditional dependencies to ad hoc version checks, and pinning application dependencies. Watch deprecation warnings so upgrades stay small, and drop Python versions once they reach end of life.