1

While installing from requirements.txt, as under:

pip install -r requirements.txt

I want to restrict to a context of Python3 only. If attempted from Python2 context, it should throw an error.

How can I accomplish the above?

Vishal
  • 3,178
  • 2
  • 34
  • 47
  • I don't think this is possible from the `requirements.txt`. You may want to look into using a `setup.py` file. [From there you can specify a `python_requires` string](https://stackoverflow.com/questions/13924931/setup-py-restrict-the-allowable-version-of-the-python-interpreter) – Patrick Haugh May 10 '18 at 00:53

1 Answers1

1

Turn your project into a proper package, and make use of the python_requires string. If you want to be absolutely sure (i.e. ensure older versions of pip will also not run under Python 2), in the setup.py include something like this before the setup call

from setuptools import setup
import sys

if sys.version_info < (3,):
    raise RuntimeError('unsupported python version')

setup(...

Naturally, declare all the dependencies in the setup.py so that other packages that depend on this one will get them without having to rely on a separate file.

If you still want to make use of requirements.txt, also add this to the following:

-e .

Which will trigger your package to be installed, thus setup.py should be invoked and then the exception will be raised which should abort the installation.

metatoaster
  • 17,419
  • 5
  • 55
  • 66