Why is django 2 available under python 2?

2019-01-12 04:50发布

According to Django 2.0 release notes Django 2.0 onwards will only support python 3, making the 1.11.X the last release series to support python 2.

See quote from the release notes page:

Django 2.0 supports Python 3.4, 3.5, and 3.6. We highly recommend and only officially support the latest release of each series.

The Django 1.11.x series is the last to support Python 2.7.

However when running pip2 install Django, django version 2 is being installed (which then fails because it assumes functionality not available in python 2):

(venv-crap) mbp15:server nir$ pip2 install django
Collecting django
  Downloading Django-2.0.tar.gz (8.0MB)
    100% |████████████████████████████████| 8.0MB 177kB/s 
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/PATH/django/setup.py", line 32, in <module>
        version = __import__('django').get_version()
      File "django/__init__.py", line 1, in <module>
        from django.utils.version import get_version
      File "django/utils/version.py", line 61, in <module>
        @functools.lru_cache()
    AttributeError: 'module' object has no attribute 'lru_cache'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/PATH/django/

I know I can manually specify requirement version below 2, making pip install a version valid for python 2, however that will complicate the installation process if I want to support both python2 and python3, and would have assumed pip will know to install only versions compatible with the python it's running from.

My questions, therefore, as the following:

  1. Why is pip attempting to install Django2 with python2 instead of automatically picking the last compatible version? Isn't that part of pips capabilities?
  2. Is there a way to make a single requirements.txt that will install Django<2.0 when running from python2 and Django>=2.0 when running with python3?

1条回答
迷人小祖宗
2楼-- · 2019-01-12 05:43

Why is pip attempting to install Django2 with python2 instead of automatically picking the last compatible version? Isn't that part of pips capabilities?

As Alasdair pointed out in the comments already, this is a known bug in Django: bug #28878.

Is there a way to make a single requirements.txt that will install Django<2.0 when running from python2 and Django>=2.0 when running with python3?

You can use the environment markers (see PEP 508):

# requirements.txt
django>=1.11,<2.0; python_version<"3.4"
django>=2.0; python_version>="3.4"

This will install one and skip another django dependency, depending on what python you are using:

$ pip2.7 install -r requirements.txt 
Ignoring django: markers 'python_version >= "3.4"' don't match your environment
Collecting django<2.0 (from -r requirements.txt (line 1))
  Downloading Django-1.11.8-py2.py3-none-any.whl (6.9MB)
...

$ pip3.6 install -r requirements.txt 
Ignoring django: markers 'python_version < "3.4"' don't match your environment
Collecting django>=2.0 (from -r requirements.txt (line 2))
  Using cached Django-2.0-py3-none-any.whl
...
查看更多
登录 后发表回答