为什么django 2在python 2下可用? [英] Why is django 2 available under python 2?

查看:67
本文介绍了为什么django 2在python 2下可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Django 2.0发行说明,Django 2.0及更高版本将仅支持python 3,使1.11.X成为支持python 2的最后一个发行系列.

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支持Python 3.4、3.5和3.6.我们强烈建议并仅正式支持每个系列的最新版本.

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.

Django 1.11.x系列是最后一个支持Python 2.7的版本.

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

但是,当运行pip2 install Django时,将安装django版本2(然后失败,因为它假定功能在python 2中不可用):

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/

我知道我可以在2以下手动指定要求版本,从而使pip安装对python 2有效的版本,但是如果我要同时支持python2和python3,这会使安装过程复杂化,并且假设pip会安装仅与从其运行的python兼容的版本.

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.

因此,我的问题如下:

  1. 为什么pip尝试使用python2安装Django2,而不是自动选择最新的兼容版本?这不是pip功能的一部分吗?
  2. 是否有一种方法可以制作一个单独的requirements.txt,当它从python2运行时可以安装Django<2.0,而当与python3运行时可以安装Django>=2.0吗?
  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?

推荐答案

为什么pip尝试使用python2安装Django2,而不是自动选择最新的兼容版本?这不是点子功能的一部分吗?

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

正如阿拉斯泰尔在评论中已经指出的那样,这是Django中的一个已知错误:

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

是否有一种方法可以创建一个Requirements.txt文件,该文件在从python2运行时将安装Django< 2.0,而在与python3运行时将安装Django> = 2.0?

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?

您可以使用环境标记(请参见 PEP 508 ):

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"

这将安装一个,并跳过另一个django依赖项,具体取决于您使用的是什么python:

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
...

这篇关于为什么django 2在python 2下可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆