six

1.17.0 · maintenance · verified Fri Mar 27

six is a Python 2 and 3 compatibility library that provides utility functions for smoothing over differences between the two Python versions, enabling codebases to run on both without modification. It consists of a single Python file (six.py) covering string types, integer types, standard library moves, metaclass helpers, and more. Current version is 1.17.0, released December 2024. The project is in maintenance mode: Python 2 reached end-of-life in 2020, and in 2024 it is entirely normal for projects to support only Python 3, so new code should not take a dependency on six — it is primarily relevant for maintaining legacy dual-version codebases or as a transitive dependency.

Warnings

Install

Imports

Quickstart

Demonstrate the most common six patterns: version guards, type checks, and stdlib moves.

import six
from six.moves import urllib
from six.moves.urllib.parse import urlencode

# Version guard
if six.PY2:
    print("Running on Python 2")
else:
    print("Running on Python 3")

# Cross-version type check
value = u"hello"
assert isinstance(value, six.text_type)        # unicode on Py2, str on Py3
assert isinstance(b"bytes", six.binary_type)  # str on Py2, bytes on Py3
assert isinstance(value, six.string_types)    # catches both str/unicode on Py2

# Stdlib move: urllib.parse.urlencode works on both versions
params = urlencode({"key": "value", "foo": "bar"})
print("Encoded:", params)

# Integer types (int + long on Py2, int only on Py3)
assert isinstance(42, six.integer_types)

# Metaclass compatibility
from six import with_metaclass
class Meta(type):
    pass
class MyBase(with_metaclass(Meta, object)):
    pass
print("Metaclass:", type(MyBase))

view raw JSON →