How do I mark a Python package as Python 2 only?

Wil*_*hes 6 python package

I have a Python package that only runs on Python 2. It has the following classifiers in its setup.py:

setup(
    # ...
    classifiers=[
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2 :: Only',
    ])
Run Code Online (Sandbox Code Playgroud)

However, if I create a virtualenv with Python 3, pip happily installs this package.

How do I prevent the package being installed? Should my setup.py throw an error based on sys.version_info? Can I stop pip even downloading the package?

Len*_*bro 8

在setup.py中,添加以下内容:

import sys
if sys.version_info[0] != 2:
    sys.stderr.write("This package only supports Python 2.\n")
    sys.exit(1)
Run Code Online (Sandbox Code Playgroud)