具体环境升级介绍
python2.7升级到python3.6
Django1.8.11升级到Django 3.0
djangorestframework3.6.3升级到djangorestframework3.10.3
# 依赖包
pymysql==0.6.7
MarkupSafe==1.0
Mako==1.0.6
requests==2.18.1
celery==3.1.25
django-celery==3.2.2
six==1.11.0
httplib2==0.9.1
supervisor==4.1.0
suds==0.4
1.安装包
python3不支持的安装包:
suds
python3 需要多安装/替代的依赖包:
anyjson
suds-jurko
其余自动安装默认最新的
2.python2到python3语法/框架升级的坑
①抛出异常
# python2
try:
pass
except ImportError, e:
pass
# python3
try:
pass
except ImportError as e:
pass
②打开文件
# python2
with open("db_helper/script/get_running_state.sh", 'r') as f:
system_status = f.read()
# python3
with open("db_helper/script/get_running_state.sh", 'rb') as f:
system_status = f.read()
③打印
# python2
print 'xxxx'
# python3
print('xxx')
④内容导入路径
# python2
import models
# python3
from apps.system import models
⑤数据库表的改变
# 原有的
template = models.ForeignKey(TasksTemplates, verbose_name=u"所属巡检任务模板")
# 升级的
template = models.ForeignKey(TasksTemplates, verbose_name=u"所属巡检任务模板", on_delete=models.CASCADE)
⑥重新生成迁移文件
⑦utf-8的改变
# python2
reload(sys)
sys.setdefaultencoding('utf8')
# python3
不需要
⑧包导入的差异
# 原有的
from django.conf.urls import include
from rest_framework.decorators import list_route
from django.utils.six.moves.urllib.parse import urlparse
from urllib import quote
from django.core.servers.basehttp import FileWrapper
from rest_framework.compat import set_rollback
from HTMLParser import HTMLParser
# 升级的
from django.urls import include
from rest_framework.decorators import action
from urllib.parse import urlparse
from urllib.parse import quote
from wsgiref.util import FileWrapper
from rest_framework.views import set_rollback
from html.parser import HTMLParser
from functools import reduce
# 改变的
# urlpatterns列表/元组不再含有字符串
# url改为path或者re_path 样例:
urlpatterns = patterns(
'sites.views',
url(r'^$', 'index'),
url(r'^healthz/$', 'healthz'),
)
urlpatterns = (
re_path('^$', views.index),
re_path('^healthz/$', views.healthz),
)
# 路由指向,使用views.+方法名
urlpatterns = patterns(
'misc.views',
url(r'^upload/$', 'upload'),
url(r'^download/$', 'download'),
)
urlpatterns = (
re_path(r'^upload/$', views.upload, name= 'upload'),
re_path(r'^download/$', views.download, name= 'download'),
)
⑨版本升级包不存在
from django.utils.decorators import available_attrs
from django.conf.urls import patterns
⑩默认配置
# 低版本的
MIDDLEWARE_CLASSES
CORS_ORIGIN_WHITELIST = (
'xxxx:443',
'xxxx:8005',
)
# 升级的
MIDDLEWARE
LANGUAGES = (
('en', 'English'),
('de', 'German'),
)
# Template加入
'django.contrib.messages.context_processors.messages'
CORS_ORIGIN_WHITELIST = (
'http://xxxx:443',
'http://xxxx:8005',
)