简体   繁体   English

为什么我在安装 Django 2.2 时无法运行“manage.py runserver”?

[英]Why can't I run “manage.py runserver” when I install Django 2.2?

I can't install Django 2.2 with pip:我无法使用 pip 安装 Django 2.2:

(django-2.2-env) fadedbee@server:/www/myproject.example.com$ pip install django==2.2
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
ERROR: Could not find a version that satisfies the requirement django==2.2 (from versions: 1.1.3, 1.1.4, 1.2, 1.2.1, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.2.6, 1.2.7, 1.3, 1.3.1, 1.3.2, 1.3.3, 1.3.4, 1.3.5, 1.3.6, 1.3.7, 1.4, 1.4.1, 1.4.2, 1.4.3, 1.4.4, 1.4.5, 1.4.6, 1.4.7, 1.4.8, 1.4.9, 1.4.10, 1.4.11, 1.4.12, 1.4.13, 1.4.14, 1.4.15, 1.4.16, 1.4.17, 1.4.18, 1.4.19, 1.4.20, 1.4.21, 1.4.22, 1.5, 1.5.1, 1.5.2, 1.5.3, 1.5.4, 1.5.5, 1.5.6, 1.5.7, 1.5.8, 1.5.9, 1.5.10, 1.5.11, 1.5.12, 1.6, 1.6.1, 1.6.2, 1.6.3, 1.6.4, 1.6.5, 1.6.6, 1.6.7, 1.6.8, 1.6.9, 1.6.10, 1.6.11, 1.7, 1.7.1, 1.7.2, 1.7.3, 1.7.4, 1.7.5, 1.7.6, 1.7.7, 1.7.8, 1.7.9, 1.7.10, 1.7.11, 1.8a1, 1.8b1, 1.8b2, 1.8rc1, 1.8, 1.8.1, 1.8.2, 1.8.3, 1.8.4, 1.8.5, 1.8.6, 1.8.7, 1.8.8, 1.8.9, 1.8.10, 1.8.11, 1.8.12, 1.8.13, 1.8.14, 1.8.15, 1.8.16, 1.8.17, 1.8.18, 1.8.19, 1.9a1, 1.9b1, 1.9rc1, 1.9rc2, 1.9, 1.9.1, 1.9.2, 1.9.3, 1.9.4, 1.9.5, 1.9.6, 1.9.7, 1.9.8, 1.9.9, 1.9.10, 1.9.11, 1.9.12, 1.9.13, 1.10a1, 1.10b1, 1.10rc1, 1.10, 1.10.1, 1.10.2, 1.10.3, 1.10.4, 1.10.5, 1.10.6, 1.10.7, 1.10.8, 1.11a1, 1.11b1, 1.11rc1, 1.11, 1.11.1, 1.11.2, 1.11.3, 1.11.4, 1.11.5, 1.11.6, 1.11.7, 1.11.8, 1.11.9, 1.11.10, 1.11.11, 1.11.12, 1.11.13, 1.11.14, 1.11.15, 1.11.16, 1.11.17, 1.11.18, 1.11.20, 1.11.21, 1.11.22, 1.11.23, 1.11.24, 1.11.25, 1.11.26, 1.11.27, 1.11.28, 1.11.29)
ERROR: No matching distribution found for django==2.2
(django-2.2-env) fadedbee@server:/www/myproject.example.

so I install it with pip3:所以我用 pip3 安装它:

(django-2.2-env) fadedbee@server:/www/myproject.example.com$ pip3 install django==2.2
Collecting django==2.2
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/54/85/0bef63668fb170888c1a2970ec897d4528d6072f32dee27653381a332642/Django-2.2-py3-none-any.whl (7.4MB)
    100% |████████████████████████████████| 7.5MB 217kB/s
Collecting sqlparse (from django==2.2)
  Using cached https://files.pythonhosted.org/packages/85/ee/6e821932f413a5c4b76be9c5936e313e4fc626b33f16e027866e1d60f588/sqlparse-0.3.1-py2.py3-none-any.whl
Collecting pytz (from django==2.2)
  Using cached https://files.pythonhosted.org/packages/e7/f9/f0b53f88060247251bf481fa6ea62cd0d25bf1b11a87888e53ce5b7c8ad2/pytz-2019.3-py2.py3-none-any.whl
Installing collected packages: sqlparse, pytz, django
Successfully installed django-2.2.12 pytz-2019.3 sqlparse-0.3.1

but I cannot run "./manage.py runserver":但我无法运行“./manage.py runserver”:

(django-2.2-env) fadedbee@server:/www/myproject.example.com$ cd example_myproject
(django-2.2-env) fadedbee@server:/www/myproject.example.com/example_myproject$ ./manage.py runserver
  File "./manage.py", line 16
    ) from exc
         ^
SyntaxError: invalid syntax

Here is the source of the generated manage.py:这是生成的manage.py的来源:

(django-2.2-env) fadedbee@server:/www/myproject.example.com/example_myproject$ cat ./manage.py
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'example_myproject.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

What am I doing wrong?我究竟做错了什么?

  • Should django==2.2 exist for pip? pip 是否应该存在 django==2.2? It looks like it does on https://pypi.org/project/Django/#history它看起来像https://pypi.org/project/Django/#history

  • Shouldn't./manage.py just work?不应该 ./manage.py 正常工作吗? (I generated it earlier, with Django 2.2, and have not edited it.) (我之前使用 Django 2.2 生成它,并没有编辑它。)

You should use a virtual environment with Python3.您应该使用 Python3 的虚拟环境。 You can do so very easily你可以很容易地做到这一点

python3 -m venv <your-virtual-env>
source <your-virtual-env>/bin/activate

You should now use this virtual environment to install and run Django您现在应该使用此虚拟环境来安装和运行 Django

(your-virtual-env) pip install django
django-admin startproject mysite
cd mysite
python manage.py runserver

Create virtual environment using python3, then activate it and install django, then manage.py will work.使用python3创建虚拟环境,然后激活它并安装django,然后manage.py就可以工作了。

$ python3 venv django-env
$ source django-env/bin/activate
$ (django-env) pip install django==2.2
$ (django-env) django-admin startproject example_myproject
$ (django-env) cd example_myproject
$ (django-env) python manage.py runserver

In your case, it appears that virtual environment 'django-2.2-env' was created using python 2.在您的情况下,虚拟环境“django-2.2-env”似乎是使用 python 2 创建的。

The last django version which supported python 2 was 1.11 as per documentation .根据文档,支持 python 2 的最后一个 django 版本是 1.11。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM