20

Is there a way to specify a python version to be used with a python package defined in setup.py?

My setup.py currently looks like this:

from distutils.core import setup
setup(
  name = 'macroetym',
  packages = ['macroetym'], # this must be the same as the name above
  version = '0.1',
  description = 'A tool for macro-etymological textual analysis.',
  author = 'Jonathan Reeve',
  author_email = 'jon.reeve@gmail.com',
  url = 'https://github.com/JonathanReeve/macro-etym', 

  download_url = 'https://github.com/JonathanReeve/macro-etym/tarball/0.1', # FIXME: make a git tag and confirm that this link works
  install_requires = ['Click', 'nltk', 'pycountry', 'pandas',
                      'matplotlib'],
  include_package_data = True,
  package_data = {'macroetym': ['etymwm-smaller.tsv']}, 
  keywords = ['nlp', 'text-analysis', 'etymology'], 
  classifiers = [],
  entry_points='''
      [console_scripts]
      macroetym = macroetym.main:cli
  ''',
)

It's a command-line program. My script runs using Python 3, but a lot of operating systems still have Python 2 as the default. How can I specify a python version to be used here? I can't seem to find anything in the docs, but maybe I'm not looking in the right place?

Jonathan
  • 10,571
  • 13
  • 67
  • 103
  • Does it *require* Python 3? If so, you could check the current version when the file is run and throw an error. – jonrsharpe May 13 '16 at 18:02

1 Answers1

28

With newer versions of setuptools (24.2.0 or above) and newer versions of pip (9.0.0 or above) you can use python_requires: https://packaging.python.org/tutorials/distributing-packages/#python-requires

Python 3+:

python_requires='>=3',

If your package is for Python 3.3 and up but you’re not willing to commit to Python 4 support yet, write:

python_requires='~=3.3',

If your package is for Python 2.6, 2.7, and all versions of Python 3 starting with 3.3, write:

python_requires='>=2.6, !=3.0.*, !=3.1.*, !=3.2.*, <4',

For older versions the old answer/workaround.

You can raise an error or warning using sys.version or platform.python_version()

import sys
print(sys.version)
print(sys.version_info)
print(sys.version_info.major)  # Returns 3 for Python 3

Or:

import platform
print(platform.python_version())
Wolph
  • 78,177
  • 11
  • 137
  • 148