Python 版本 2.7.18

在 Python 中有很多 RESTful Web 框架,如 Django REST framework,Flask-RESTful,FastAPI等等。fastapi 需要 Python 3.6+,本次环境不支持。。。

本次使用 Django rest framework

django-rest-framework https://www.django-rest-framework.org/

配置依赖

django,djangorestframework 等

  1. C:\xiaohui\python-u8\demo\web\fastapi>pip install djangorestframework
  2. DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python
  3. 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
  4. Collecting djangorestframework
  5. Downloading https://files.pythonhosted.org/packages/1b/fe/fcebec2a98fbd1548da1c12ce8d7f634a02a9cce350833fa227a625ec624/djangorestframework-3.9.4-py2.py3-none-any.whl (911kB)
  6. |████████████████████████████████| 921kB 19kB/s
  7. Installing collected packages: djangorestframework
  8. Successfully installed djangorestframework-3.9.4
  9. WARNING: You are using pip version 19.2.3, however version 20.3.4 is available.
  10. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
  11. C:\xiaohui\python-u8\demo\web\fastapi>
  12. C:\xiaohui\python-u8\demo\web\fastapi>
  13. C:\xiaohui\python-u8\demo\web\fastapi>pip list
  14. DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python
  15. 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
  16. Package Version
  17. ----------------------------- -------
  18. astroid 1.6.6
  19. autopep8 1.6.0
  20. backports.functools-lru-cache 1.6.4
  21. colorama 0.4.4
  22. configparser 4.0.2
  23. Django 1.11.29
  24. djangorestframework 3.9.4
  25. enum34 1.1.10
  26. futures 3.3.0
  27. isort 4.3.21
  28. lazy-object-proxy 1.6.0
  29. mccabe 0.6.1
  30. pip 19.2.3
  31. pycodestyle 2.8.0
  32. pycparser 2.21
  33. pylint 1.9.5
  34. pythonnet 2.5.2
  35. pytz 2021.3
  36. setuptools 41.2.0
  37. singledispatch 3.7.0
  38. six 1.16.0
  39. toml 0.10.2
  40. wrapt 1.13.3
  41. WARNING: You are using pip version 19.2.3, however version 20.3.4 is available.
  42. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
  43. C:\xiaohui\python-u8\demo\web\fastapi>

重新开始

环境 python 2.7.18 64bit
使用 virtualenv 创建的 python2.7.18 来测试 Django、django-rest-framework

  1. # 激活python 虚拟环境 python2.7.18
  2. PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts> .\activate
  3. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts>
  4. # 查看已安装的python 依赖
  5. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts> pip list
  6. 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. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
  7. Package Version
  8. ---------- ---------
  9. certifi 2021.10.8
  10. minio 7.1.3
  11. pip 20.3.4
  12. setuptools 44.1.1
  13. urllib3 1.26.8
  14. wheel 0.37.1
  15. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts>
  16. # 安装 Django、djangorestframework
  17. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts> pip install django djangorestframework
  18. 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. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can
  19. be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
  20. Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
  21. Collecting django
  22. Downloading https://mirrors.aliyun.com/pypi/packages/49/49/178daa8725d29c475216259eb19e90b2aa0b8c0431af8c7e9b490ae6481d/Django-1.11.29-py2.py3-none-any.whl (6.9 MB)
  23. |████████████████████████████████| 6.9 MB 5.4 MB/s
  24. Collecting djangorestframework
  25. Downloading https://mirrors.aliyun.com/pypi/packages/1b/fe/fcebec2a98fbd1548da1c12ce8d7f634a02a9cce350833fa227a625ec624/djangorestframework-3.9.4-py2.py3-none-any.whl (911 kB)
  26. |████████████████████████████████| 911 kB 5.1 MB/s
  27. Collecting pytz
  28. Downloading https://mirrors.aliyun.com/pypi/packages/d3/e3/d9f046b5d1c94a3aeab15f1f867aa414f8ee9d196fae6865f1d6a0ee1a0b/pytz-2021.3-py2.py3-none-any.whl (503 kB)
  29. |████████████████████████████████| 503 kB 5.4 MB/s
  30. Installing collected packages: pytz, django, djangorestframework
  31. Successfully installed django-1.11.29 djangorestframework-3.9.4 pytz-2021.3
  32. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts>
  33. # 查看已安装的 django、djangorestframework
  34. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts> pip list
  35. 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. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can
  36. be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
  37. Package Version
  38. ------------------- ---------
  39. certifi 2021.10.8
  40. Django 1.11.29
  41. djangorestframework 3.9.4
  42. minio 7.1.3
  43. pip 20.3.4
  44. pytz 2021.3
  45. setuptools 44.1.1
  46. urllib3 1.26.8
  47. wheel 0.37.1
  48. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts>

创建 Django 项目 django-admin startproject my_django

  1. # django-admin startproject my_django 创建项目 my_django
  2. # 注意项目名称只能是字母、数字、下划线,其它的该本版要报错
  3. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env>django-admin startproject my_django
  4. # 查看看目录
  5. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env> dir
  6. 目录: D:\OtherProjects\other\python\python_env27\python27_env
  7. Mode LastWriteTime Length Name
  8. ---- ------------- ------ ----
  9. d----- 2022/2/12 15:16 Include
  10. d----- 2022/2/12 15:31 Lib
  11. d----- 2022/2/12 15:16 libs
  12. d----- 2022/2/15 10:21 my_django
  13. d----- 2022/2/15 10:15 Scripts
  14. -a---- 2022/2/12 15:16 40 .gitignore
  15. -a---- 2022/2/12 15:16 294 pyvenv.cfg
  16. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env>
  17. # 查看目录
  18. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env> dir
  19. 目录: D:\OtherProjects\other\python\python_env27\python27_env
  20. Mode LastWriteTime Length Name
  21. ---- ------------- ------ ----
  22. d----- 2022/2/12 15:16 Include
  23. d----- 2022/2/12 15:31 Lib
  24. d----- 2022/2/12 15:16 libs
  25. d----- 2022/2/15 10:21 my_django
  26. d----- 2022/2/15 10:15 Scripts
  27. -a---- 2022/2/12 15:16 40 .gitignore
  28. -a---- 2022/2/12 15:16 294 pyvenv.cfg
  29. # 进入项目
  30. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env> cd .\my_django\
  31. # 查看结构
  32. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\my_django> tree /F
  33. 文件夹 PATH 列表
  34. 卷序列号为 34D6-1340
  35. D:.
  36. manage.py
  37. └─my_django
  38. settings.py
  39. urls.py
  40. wsgi.py
  41. __init__.py
  42. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\my_django>

题外话 tree 命令不显示文件

  1. # 查看 tree 的帮助,Windows command /?
  2. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\my_django> tree /?
  3. 以图形显示驱动器或路径的文件夹结构。
  4. TREE [drive:][path] [/F] [/A]
  5. /F 显示每个文件夹中文件的名称。
  6. /A 使用 ASCII 字符,而不使用扩展字符。
  7. # tree 加上参数,就可以显示文件了
  8. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\my_django> tree /F /A
  9. 文件夹 PATH 列表
  10. 卷序列号为 34D6-1340
  11. D:.
  12. | db.sqlite3
  13. | manage.py
  14. |
  15. \---my_django
  16. settings.py
  17. settings.pyc
  18. urls.py
  19. urls.pyc
  20. wsgi.py
  21. wsgi.pyc
  22. __init__.py
  23. __init__.pyc
  24. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\my_django>

运行项目 python .\manage.py runserver 8000

  1. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\my_django> python .\manage.py runserver 8000
  2. Performing system checks...
  3. System check identified no issues (0 silenced).
  4. You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
  5. Run 'python manage.py migrate' to apply them.
  6. February 15, 2022 - 10:35:51
  7. Django version 1.11.29, using settings 'my_django.settings'
  8. Starting development server at http://127.0.0.1:8000/
  9. Quit the server with CTRL-BREAK.

访问服务 http://localhost:8000

访问 http://localhost:8000/admin,或者 http://localhost:8000

先不要管页面的内容,接着往下走

创建应用 python manage.py startapp my_rest_framework

  1. # 创建应用 python .\manage.py startapp my_rest_framework
  2. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\my_django> python .\manage.py startapp my_rest_framework
  3. # 查看结构 tree /F
  4. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\my_django> tree /F
  5. 文件夹 PATH 列表
  6. 卷序列号为 34D6-1340
  7. D:.
  8. db.sqlite3
  9. manage.py
  10. ├─my_django
  11. settings.py
  12. settings.pyc
  13. urls.py
  14. urls.pyc
  15. wsgi.py
  16. wsgi.pyc
  17. __init__.py
  18. __init__.pyc
  19. └─my_rest_framework
  20. admin.py
  21. apps.py
  22. models.py
  23. tests.py
  24. views.py
  25. __init__.py
  26. └─migrations
  27. __init__.py
  28. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\my_django>

将创建的应用 my_rest_framework 添加到 settings.py

修改 settings.py 文件,为 INSTALLED_APPS 追加 my_rest_framework

  1. INSTALLED_APPS = [
  2. 'django.contrib.admin',
  3. 'django.contrib.auth',
  4. 'django.contrib.contenttypes',
  5. 'django.contrib.sessions',
  6. 'django.contrib.messages',
  7. 'django.contrib.staticfiles',
  8. "my_rest_framework",
  9. ]

my_rest_framework 目录下修改 views.py

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.shortcuts import render
  4. # 导入 rest_framework 相关依赖
  5. from rest_framework.views import APIView
  6. from rest_framework.response import Response
  7. from rest_framework.decorators import api_view
  8. from django.http import JsonResponse, HttpResponseNotAllowed, HttpResponse
  9. # Create your views here.
  10. @api_view(['GET'])
  11. def hello(request):
  12. if request.method == 'GET':
  13. content = "hello world"
  14. return JsonResponse(data=content, safe=False)

my_rest_framework 目录下创建 urls.py

  1. #!/usr/bin/python
  2. # coding=utf-8
  3. from django.contrib import admin
  4. from django.conf.urls import url, include
  5. from . import views
  6. urlpatterns = [
  7. url('hello/', views.hello)
  8. ]

将应用 my_rest_framework 的 urls 添加入 项目的 urls.py 中

  1. """my_django URL Configuration
  2. The `urlpatterns` list routes URLs to views. For more information please see:
  3. https://docs.djangoproject.com/en/1.11/topics/http/urls/
  4. Examples:
  5. Function views
  6. 1. Add an import: from my_app import views
  7. 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
  8. Class-based views
  9. 1. Add an import: from other_app.views import Home
  10. 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
  11. Including another URLconf
  12. 1. Import the include() function: from django.conf.urls import url, include
  13. 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
  14. """
  15. from django.conf.urls import url, include
  16. from django.contrib import admin
  17. urlpatterns = [
  18. url(r'^admin/', admin.site.urls),
  19. # 添加 my_rest_framework 的 urls
  20. url('', include("my_rest_framework.urls")),
  21. ]

接着就是 python manage.py runserver 8000,然后 postman 访问 http://localhost:8000/hello/

其实存在三种方式实现 restful 接口

方式一:上例中的 api_view 注解

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.shortcuts import render
  4. # 导入 rest_framework 相关依赖
  5. from rest_framework.views import APIView
  6. from rest_framework.response import Response
  7. from rest_framework.decorators import api_view
  8. from django.http import JsonResponse, HttpResponseNotAllowed, HttpResponse
  9. # Create your views here.
  10. @api_view(['GET'])
  11. def hello(request):
  12. if request.method == 'GET':
  13. content = "hello world"
  14. return JsonResponse(data=content, safe=False)

方式二:继承 APIView 方式

api_view.py

  1. # -*- coding: UTF-8 -*-
  2. '''
  3. django rest framework 的 APIView 实现 restful api
  4. '''
  5. from django.shortcuts import render
  6. from rest_framework.views import APIView
  7. from rest_framework.response import Response
  8. # Create your views here.
  9. class MyApiView(APIView):
  10. '''
  11. 使用 APIView 方式实现 restful 接口
  12. '''
  13. def get(self, request):
  14. a = request.GET['a']
  15. res = {
  16. 'code': 100,
  17. 'data': a,
  18. 'msg': 'success, get',
  19. }
  20. return Response(res)
  21. def post(self, request):
  22. a = request.GET['a']
  23. res = {
  24. 'code': 100,
  25. 'data': a,
  26. 'msg': 'success, post',
  27. }
  28. return Response(res)
  29. def delete(self, request):
  30. a = request.GET['a']
  31. res = {
  32. 'code': 100,
  33. 'data': a,
  34. 'msg': 'success,delete',
  35. }
  36. return Response(res)
  37. def put(self, request):
  38. a = request.GET['a']
  39. res = {
  40. 'code': 100,
  41. 'data': a,
  42. 'msg': 'success, put',
  43. }
  44. return Response(res)

修改应用的 urls.py

  1. #!/usr/bin/python
  2. # coding=utf-8
  3. from django.contrib import admin
  4. from django.conf.urls import url, include
  5. from . import views, api_views
  6. urlpatterns = [
  7. # 普通 @api_view 方式
  8. url('hello/', views.hello),
  9. # PAIView 方式
  10. url('apiview/', api_views.MyApiView.as_view()),
  11. ]

注意,访问时候需要加上末尾的 / , eg: http://localhost:8000/apiview/?a=1

方式三:viewsets,继承 viewsets.ModelViewSet

view_sets.py

  1. #!/usr/bin/python
  2. # coding=utf-8
  3. from django.shortcuts import render
  4. from rest_framework.response import Response
  5. from rest_framework import viewsets
  6. from rest_framework.decorators import list_route
  7. import json
  8. import uuid
  9. class DemoViewSets(viewsets.ModelViewSet):
  10. '''
  11. 使用 viewsets 来做 restful
  12. '''
  13. @list_route(methods=['GET',],)
  14. def get(self, request):
  15. data = json.loads(request.body)
  16. print(data)
  17. res = {
  18. 'code': '100',
  19. 'msg': 'success get',
  20. 'data': data,
  21. }
  22. return Response(data=res)
  23. @list_route(methods=['PUT',],)
  24. def get(self, request):
  25. data = json.loads(request.body)
  26. print(data)
  27. res = {
  28. 'code': '100',
  29. 'msg': 'success put',
  30. 'data': data,
  31. }
  32. return Response(data=res)
  33. @list_route(methods=['POST',],)
  34. def get(self, request):
  35. data = json.loads(request.body)
  36. print(data)
  37. res = {
  38. 'code': '100',
  39. 'msg': 'success post',
  40. 'data': data,
  41. }
  42. return Response(data=res)
  43. @list_route(methods=['DELETE',],)
  44. def get(self, request):
  45. data = json.loads(request.body)
  46. print(data)
  47. res = {
  48. 'code': '100',
  49. 'msg': 'success delete',
  50. 'data': data,
  51. }
  52. return Response(data=res)

修改应用中的 urls.py

  1. #!/usr/bin/python
  2. # coding=utf-8
  3. from django.contrib import admin
  4. from django.conf.urls import url, include
  5. from rest_framework.routers import DefaultRouter
  6. from . import views, api_views, view_sets
  7. router = DefaultRouter()
  8. router.register(prefix=r'', viewset=view_sets.DemoViewSets, base_name='view_sets_demo')
  9. urlpatterns = [
  10. # 普通 @api_view 方式
  11. url('hello/', views.hello),
  12. # PAIView 方式
  13. url('apiview/', api_views.MyApiView.as_view()),
  14. # viewsets 方式
  15. url('viewsets/', include(router.urls)),
  16. ]

访问的路径是:http://localhost:8000/viewsets/get/?a=1

有点没搞懂 请求要加上方法名,可是改变请求方式,居然也能成功?难道说每个方法可以通用于任意的请求方式?

swagger 加入 drf_yasg

安装 drf-yasg

出错

  1. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts> pip install drf-yasg
  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. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can
  3. be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
  4. Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
  5. Collecting drf-yasg
  6. Downloading https://mirrors.aliyun.com/pypi/packages/18/de/729c403c2ccfef3680790c2cb5460b7bdf93457586f09d5c15a9e5da450f/drf_yasg-1.17.1-py2.py3-none-any.whl (1.1 MB)
  7. |████████████████████████████████| 1.1 MB 4.3 MB/s
  8. Requirement already satisfied: six>=1.10.0 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from drf-yasg) (1.16.0)
  9. Requirement already satisfied: coreapi>=2.3.3 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from drf-yasg) (2.3.3)
  10. Requirement already satisfied: Django>=1.11.7 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from drf-yasg) (1.11.29)
  11. Collecting ruamel.yaml>=0.15.34
  12. Downloading https://mirrors.aliyun.com/pypi/packages/65/e9/494f4a4ae771028900ff085d41f39c7f117b9e839ad8c15c2101ca4e16f1/ruamel.yaml-0.17.1-py2.py3-none-any.whl (101 kB)
  13. |████████████████████████████████| 101 kB 2.8 MB/s
  14. Requirement already satisfied: uritemplate>=3.0.0 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from drf-yasg) (4.1.0)
  15. Requirement already satisfied: djangorestframework>=3.8 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from drf-yasg) (3.9.4)
  16. Requirement already satisfied: coreschema>=0.0.4 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from drf-yasg) (0.0.4)
  17. Collecting inflection>=0.3.1
  18. Using cached https://mirrors.aliyun.com/pypi/packages/d5/35/a6eb45b4e2356fe688b21570864d4aa0d0a880ce387defe9c589112077f8/inflection-0.3.1.tar.gz (8.7 kB)
  19. Collecting packaging
  20. Downloading https://mirrors.aliyun.com/pypi/packages/3e/89/7ea760b4daa42653ece2380531c90f64788d979110a2ab51049d92f408af/packaging-20.9-py2.py3-none-any.whl (40 kB)
  21. |████████████████████████████████| 40 kB 3.7 MB/s
  22. Requirement already satisfied: requests in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from coreapi>=2.3.3->drf-yasg) (2.27.1)
  23. Requirement already satisfied: itypes in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from coreapi>=2.3.3->drf-yasg) (1.2.0)
  24. Requirement already satisfied: pytz in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from Django>=1.11.7->drf-yasg) (2021.3)
  25. Collecting ruamel.yaml.clib>=0.1.2; platform_python_implementation == "CPython" and python_version < "3.10"
  26. Using cached https://mirrors.aliyun.com/pypi/packages/b7/81/c04fb9be62657d4dce8aa2d99fde258a3af1cd77ec72af525593e9560127/ruamel.yaml.clib-0.2.4.tar.gz (180 kB)
  27. ERROR: Command errored out with exit status 1:
  28. command: 'D:\OtherProjects\other\python\python_env27\python27_env\Scripts\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'c:\\users\\dell\\appdata\\local\\temp\\pip-install-hezagj\\ruamel-yaml-clib\\setup.py'"'"'; __file__='"'"'c:\\users\\dell\\appdata\\local\\temp\\pip-install-hezagj\\ruamel-yaml-clib\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'c:\users\dell\appdata\local\temp\pip-pip-egg-info-ivur0p'
  29. cwd: c:\users\dell\appdata\local\temp\pip-install-hezagj\ruamel-yaml-clib\
  30. Complete output (1 lines):
  31. minimum python version(s): [(3, 5)]
  32. ----------------------------------------
  33. ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
  34. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts>

上述错误时 ruamel-yaml-clib 安装失败。只好晚上查询资料,ruamel-yaml-clib==0.2.0 的支持 python27 64bit的。那么手动安装一下吧 pip install ruamel-yaml-clib==0.2.0

安装 ruamel-yaml-clib==0.2.0

  1. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts> pip install ruamel-yaml-clib==0.2.0
  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. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can
  3. be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
  4. Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
  5. Collecting ruamel-yaml-clib==0.2.0
  6. Downloading https://mirrors.aliyun.com/pypi/packages/97/f4/98dc93e36bfdaefa8edf5183fbf14a00439f48e5cd301e7c2954854b11c2/ruamel.yaml.clib-0.2.0-cp27-cp27m-win_amd64.whl (115 kB)
  7. |████████████████████████████████| 115 kB 3.1 MB/s
  8. Installing collected packages: ruamel-yaml-clib
  9. Successfully installed ruamel-yaml-clib

再次安装 drf-yasg

  1. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts> pip install drf-yasg
  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. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can
  3. be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
  4. Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
  5. Collecting drf-yasg
  6. Using cached https://mirrors.aliyun.com/pypi/packages/18/de/729c403c2ccfef3680790c2cb5460b7bdf93457586f09d5c15a9e5da450f/drf_yasg-1.17.1-py2.py3-none-any.whl (1.1 MB)
  7. Requirement already satisfied: six>=1.10.0 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from drf-yasg) (1.16.0)
  8. Requirement already satisfied: coreapi>=2.3.3 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from drf-yasg) (2.3.3)
  9. Requirement already satisfied: Django>=1.11.7 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from drf-yasg) (1.11.29)
  10. Collecting ruamel.yaml>=0.15.34
  11. Using cached https://mirrors.aliyun.com/pypi/packages/65/e9/494f4a4ae771028900ff085d41f39c7f117b9e839ad8c15c2101ca4e16f1/ruamel.yaml-0.17.1-py2.py3-none-any.whl (101 kB)
  12. Requirement already satisfied: uritemplate>=3.0.0 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from drf-yasg) (4.1.0)
  13. Requirement already satisfied: djangorestframework>=3.8 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from drf-yasg) (3.9.4)
  14. Requirement already satisfied: coreschema>=0.0.4 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from drf-yasg) (0.0.4)
  15. Collecting inflection>=0.3.1
  16. Using cached https://mirrors.aliyun.com/pypi/packages/d5/35/a6eb45b4e2356fe688b21570864d4aa0d0a880ce387defe9c589112077f8/inflection-0.3.1.tar.gz (8.7 kB)
  17. Collecting packaging
  18. Using cached https://mirrors.aliyun.com/pypi/packages/3e/89/7ea760b4daa42653ece2380531c90f64788d979110a2ab51049d92f408af/packaging-20.9-py2.py3-none-any.whl (40 kB)
  19. Requirement already satisfied: requests in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from coreapi>=2.3.3->drf-yasg) (2.27.1)
  20. Requirement already satisfied: itypes in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from coreapi>=2.3.3->drf-yasg) (1.2.0)
  21. Requirement already satisfied: pytz in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from Django>=1.11.7->drf-yasg) (2021.3)
  22. Requirement already satisfied: ruamel.yaml.clib>=0.1.2; platform_python_implementation == "CPython" and python_version < "3.10" in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from ruamel.yaml>=0.15.34->drf-yasg) (0.2.0)
  23. Requirement already satisfied: jinja2 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from coreschema>=0.0.4->drf-yasg) (2.11.3)
  24. Collecting pyparsing>=2.0.2
  25. Downloading https://mirrors.aliyun.com/pypi/packages/8a/bb/488841f56197b13700afd5658fc279a2025a39e22449b7cf29864669b15d/pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)
  26. |████████████████████████████████| 67 kB 3.3 MB/s
  27. Requirement already satisfied: chardet<5,>=3.0.2; python_version < "3" in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from requests->coreapi>=2.3.3->drf-yasg) (4.0.0)
  28. Requirement already satisfied: idna<3,>=2.5; python_version < "3" in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from requests->coreapi>=2.3.3->drf-yasg) (2.10)
  29. Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from requests->coreapi>=2.3.3->drf-yasg) (1.26.8)
  30. Requirement already satisfied: certifi>=2017.4.17 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from requests->coreapi>=2.3.3->drf-yasg) (2021.10.8)
  31. Requirement already satisfied: MarkupSafe>=0.23 in d:\otherprojects\other\python\python_env27\python27_env\lib\site-packages (from jinja2->coreschema>=0.0.4->drf-yasg) (1.1.1)
  32. Building wheels for collected packages: inflection
  33. Building wheel for inflection (setup.py) ... done
  34. Created wheel for inflection: filename=inflection-0.3.1-py2-none-any.whl size=6095 sha256=19ee270a099848e8618da0194578c7ebc642d6fbbac392496f1c274550787b94
  35. Stored in directory: c:\users\dell\appdata\local\pip\cache\wheels\7c\49\a1\498800189565b8e8708fed7db2fbdacf3844a38396f10a39e3
  36. Successfully built inflection
  37. Installing collected packages: ruamel.yaml, inflection, pyparsing, packaging, drf-yasg
  38. Successfully installed drf-yasg-1.17.1 inflection-0.3.1 packaging-20.9 pyparsing-2.4.7 ruamel.yaml-0.17.1
  39. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts>

查看安装的 package

  1. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts> pip list
  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. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can
  3. be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
  4. Package Version
  5. ------------------- ---------
  6. certifi 2021.10.8
  7. chardet 4.0.0
  8. coreapi 2.3.3
  9. coreschema 0.0.4
  10. Django 1.11.29
  11. djangorestframework 3.9.4
  12. drf-yasg 1.17.1
  13. idna 2.10
  14. inflection 0.3.1
  15. itypes 1.2.0
  16. Jinja2 2.11.3
  17. MarkupSafe 1.1.1
  18. minio 7.1.3
  19. packaging 20.9
  20. pip 20.3.4
  21. pyparsing 2.4.7
  22. pytz 2021.3
  23. requests 2.27.1
  24. ruamel.yaml 0.17.1
  25. ruamel.yaml.clib 0.2.0
  26. setuptools 44.1.1
  27. six 1.16.0
  28. uritemplate 4.1.0
  29. urllib3 1.26.8
  30. wheel 0.37.1
  31. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\Scripts>

使用 drf-yasg

参考:https://github.com/axnsan12/drf-yasg
https://drf-yasg.readthedocs.io/en/stable/readme.html
修改 settings.py 加入 drf_yasg

  1. # Application definition
  2. INSTALLED_APPS = [
  3. 'django.contrib.admin',
  4. 'django.contrib.auth',
  5. 'django.contrib.contenttypes',
  6. 'django.contrib.sessions',
  7. 'django.contrib.messages',
  8. 'django.contrib.staticfiles',
  9. "my_rest_framework",
  10. "drf_yasg",
  11. ]

修改项目的 urls.py

  1. #!/usr/bin/python
  2. # coding=utf-8
  3. from unicodedata import name
  4. from django.contrib import admin
  5. from django.conf.urls import url, include
  6. from rest_framework import permissions
  7. from drf_yasg.views import get_schema_view
  8. from drf_yasg import openapi
  9. from rest_framework.routers import DefaultRouter
  10. from . import views, api_views, view_sets
  11. schema_view = get_schema_view(
  12. info=openapi.Info(title="123",
  13. default_version='v1',
  14. description="def-yasg Test description",
  15. terms_of_service="https://www.google.com/policies/terms/",
  16. contact=openapi.Contact(email="xuxiaohuimail@163.com", name="123",),
  17. license=openapi.License(name="BSD License"),
  18. ),
  19. public=True,
  20. permission_classes=[permissions.AllowAny,],
  21. )
  22. router = DefaultRouter()
  23. router.register(prefix=r'', viewset=view_sets.DemoViewSets, base_name='view_sets_demo')
  24. urlpatterns = [
  25. # swagger 相关的url
  26. # url(r'swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
  27. # url (r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
  28. # url(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
  29. # 普通 @api_view 方式
  30. url('hello/', views.hello),
  31. # PAIView 方式
  32. url('apiview/', api_views.MyApiView.as_view()),
  33. # viewsets 方式
  34. url('viewsets/', include(router.urls)),
  35. ]

悲愤的事情发生了,启动报错:

  1. (python27_env) PS D:\OtherProjects\other\python\python_env27\python27_env\my_django> python .\manage.py runserver 8000
  2. Performing system checks...
  3. Unhandled exception in thread started by <function wrapper at 0x0000000004718A58>
  4. Traceback (most recent call last):
  5. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\django\utils\autoreload.py", line 228, in wrapper
  6. fn(*args, **kwargs)
  7. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\django\core\management\commands\runserver.py", line 124, in inner_run
  8. self.check(display_num_errors=True)
  9. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\django\core\management\base.py", line 359, in check
  10. include_deployment_checks=include_deployment_checks,
  11. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\django\core\management\base.py", line 346, in _run_checks
  12. return checks.run_checks(**kwargs)
  13. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
  14. new_errors = check(app_configs=app_configs)
  15. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config
  16. return check_resolver(resolver)
  17. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver
  18. return check_method()
  19. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\django\urls\resolvers.py", line 256, in check
  20. for pattern in self.url_patterns:
  21. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\django\utils\functional.py", line 35, in __get__
  22. res = instance.__dict__[self.name] = self.func(instance)
  23. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\django\urls\resolvers.py", line 407, in url_patterns
  24. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  25. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\django\utils\functional.py", line 35, in __get__
  26. res = instance.__dict__[self.name] = self.func(instance)
  27. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\django\urls\resolvers.py", line 400, in urlconf_module
  28. return import_module(self.urlconf_name)
  29. File "D:\devTools\Python\Python27\lib\importlib\__init__.py", line 37, in import_module
  30. __import__(name)
  31. File "D:\OtherProjects\other\python\python_env27\python27_env\my_django\my_django\urls.py", line 21, in <module>
  32. url('', include("my_rest_framework.urls")),
  33. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\django\conf\urls\__init__.py", line 50, in include
  34. urlconf_module = import_module(urlconf_module)
  35. File "D:\devTools\Python\Python27\lib\importlib\__init__.py", line 37, in import_module
  36. __import__(name)
  37. File "D:\OtherProjects\other\python\python_env27\python27_env\my_django\my_rest_framework\urls.py", line 7, in <module>
  38. from rest_framework import permissions
  39. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\rest_framework\permissions.py", line 9, in <module>
  40. from rest_framework import exceptions
  41. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\rest_framework\exceptions.py", line 18, in <module>
  42. from rest_framework.compat import unicode_to_repr
  43. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\rest_framework\compat.py", line 132, in <module>
  44. import coreapi
  45. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\coreapi\__init__.py", line 2, in <module>
  46. from coreapi import auth, codecs, exceptions, transports, utils
  47. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\coreapi\transports\__init__.py", line 3, in <module>
  48. from coreapi.transports.http import HTTPTransport
  49. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\coreapi\transports\http.py", line 13, in <module>
  50. import uritemplate
  51. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\uritemplate\__init__.py", line 28, in <module>
  52. from uritemplate.api import (
  53. File "D:\OtherProjects\other\python\python_env27\python27_env\lib\site-packages\uritemplate\api.py", line 19
  54. uri: str,
  55. ^
  56. SyntaxError: invalid syntax

对于报错,万分无赖,只好查看自己成功的项目的依赖包

requirement.txt

  1. astroid==1.6.6
  2. autopep8==1.6.0
  3. backports.functools-lru-cache==1.6.4
  4. certifi==2021.10.8
  5. chardet==4.0.0
  6. colorama==0.4.4
  7. configparser==4.0.2
  8. coreapi==2.3.3
  9. coreschema==0.0.4
  10. Django==1.11.29
  11. djangorestframework==3.9.4
  12. drf-yasg==1.17.1
  13. enum34==1.1.10
  14. futures==3.3.0
  15. idna==2.10
  16. inflection==0.3.1
  17. isort==4.3.21
  18. itypes==1.2.0
  19. Jinja2==2.11.3
  20. lazy-object-proxy==1.6.0
  21. MarkupSafe==1.1.1
  22. mccabe==0.6.1
  23. packaging==20.9
  24. pycodestyle==2.8.0
  25. pycparser==2.21
  26. pylint==1.9.5
  27. pyparsing==2.4.7
  28. pythonnet==2.5.2
  29. pytz==2021.3
  30. requests==2.27.1
  31. ruamel.ordereddict==0.4.15
  32. ruamel.yaml==0.16.13
  33. ruamel.yaml.clib==0.2.2
  34. singledispatch==3.7.0
  35. six==1.16.0
  36. toml==0.10.2
  37. uritemplate==3.0.1
  38. urllib3==1.26.8
  39. wrapt==1.13.3

使用 pip install -r requirement.txt 安装

然后再执行 python .\manage.py runserver 8000

神奇的事情出现,居然可以了。。。。定位了几个小时的事情,突然就好了