Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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',
    ])

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?

like image 387
Wilfred Hughes Avatar asked Oct 09 '14 12:10

Wilfred Hughes


People also ask

How do I know if a script is Python 2 or 3?

If you want to determine whether Python2 or Python3 is running, you can check the major version with this sys. version_info. major . 2 means Python2, and 3 means Python3.

How do I install Python 2 pip packages?

pip installation To use pip, first install a custom version of Python 2. pip is then installed with it. You can then use the pip command to create a virtualenv and install modules.


1 Answers

In setup.py, add this:

import sys
if sys.version_info[0] != 2:
    sys.stderr.write("This package only supports Python 2.\n")
    sys.exit(1)
like image 139
Lennart Regebro Avatar answered Oct 13 '22 21:10

Lennart Regebro