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

查看:15
本文介绍了为什么 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 install 一个对 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 来安装 Django<2.0 从 python2 和 Django>=2.0 运行时使用 python3 运行时?
  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 而不是自动选择最后一个兼容版本?这不是 pips 功能的一部分吗?

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

正如 Alasdair 已经在评论中指出的那样,这是 Django 中的一个已知错误:错误 #28878.

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天全站免登陆