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

与世无争的帅哥 提交于 2020-07-23 07:16:18

问题


I can't install Django 2.2 with pip:

(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:

(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":

(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:

(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? It looks like it does on https://pypi.org/project/Django/#history

  • Shouldn't ./manage.py just work? (I generated it earlier, with Django 2.2, and have not edited it.)


回答1:


You should use a virtual environment with 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

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



回答2:


Create virtual environment using python3, then activate it and install django, then manage.py will work.

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

The last django version which supported python 2 was 1.11 as per documentation.



来源:https://stackoverflow.com/questions/61338135/why-cant-i-run-manage-py-runserver-when-i-install-django-2-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!