【Python2转换升级Python3异常错误更改】(四)

目录

33.ImportError: cannot import name 'six' from 'django.utils'

34.ModuleNotFoundError: No module named 'django.utils.six'

35.ImportError: cannot import name 'cookie_date' from 'django.utils.http'

36.ImportError: cannot import name 'urlresolvers' from 'django.core'

37.ImportError: cannot import name 'patterns' from 'django.conf.urls'

38.ModuleNotFoundError: No module named 'copy_reg'

39.NameError: name 'unicode' is not defined

40.AttributeError: module 'types' has no attribute 'ClassType'

41.AttributeError: module 'types' has no attribute 'InstanceType'

42.ValueError: The 'hive' package was not installed in a way that PackageLoader understands.

43.ImportError: cannot import name 'soft_unicode' from 'markupsafe'

44.AttributeError: module 'urllib' has no attribute 'quote'

45.django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:

?: (urls.E007) The custom handler404 view 'hive.shared.custom_404_view' does not take the correct number of arguments (request, exception).

46.create user_operation_log faild: module 'jwt' has no attribute 'decode'


33.ImportError: cannot import name 'six' from 'django.utils'

解决:
改为
import six

34.ModuleNotFoundError: No module named 'django.utils.six'

six.py放入django.utils下

35.ImportError: cannot import name 'cookie_date' from 'django.utils.http'

解决:
python3 django3不支持cookie_date 改为
http_date

36.ImportError: cannot import name 'urlresolvers' from 'django.core'

解决:
django1.3以后不支持 urlresolvers 改为
from django.urls import reverse

37.ImportError: cannot import name 'patterns' from 'django.conf.urls'

解决:
从1.10后django后patterns被移除了,已经没有这个模块了。使用 django 1.10 以后的版本需要改用 django.conf.urls.url() 
from django.conf.urls import patterns,include,url
from django.contrib import admin
from helloworld.views import hello
​
示例:
from django.conf.urls import patterns,include,url
urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^helloworld$', hello),
)
改为
from django.conf.urls import url,include
​
urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^helloworld$', hello),
]

38.ModuleNotFoundError: No module named 'copy_reg'

解决:
copy_reg 在python3 被废除
使用
copyreg

39.NameError: name 'unicode' is not defined

解决:
# python 3 doesn't have a unicode type
try:
    unicode
except:
    unicode = str

40.AttributeError: module 'types' has no attribute 'ClassType'

解决:
/usr/local/python3/lib/python3.7/types.py 加
class _C:
    def _m(self): pass
ClassType = type(_C)

41.AttributeError: module 'types' has no attribute 'InstanceType'

解决:
/usr/local/python3/lib/python3.7/types.py 加
_x = _C()
InstanceType = type(_x)

42.ValueError: The 'hive' package was not installed in a way that PackageLoader understands.

解决:
jinja2原本为3.1.2版本,降低版本至2.11.2
pip3 install jinja2==2.11.2

43.ImportError: cannot import name 'soft_unicode' from 'markupsafe'

解决:
soft_unicode 改为 soft_str

44.AttributeError: module 'urllib' has no attribute 'quote'

解决:
from urllib.parse import quote
urllib.parse 导入quote

45.django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:

?: (urls.E007) The custom handler404 view 'hive.shared.custom_404_view' does not take the correct number of arguments (request, exception).

如图新增代码解决问题

46.create user_operation_log faild: module 'jwt' has no attribute 'decode'

解决:
# jwt卸载命令
pip uninstall jwt
# 保险起见,将PyJWT一同卸载
pip uninstall PyJWT
# 重新安装PyJWT
pip install PyJWT

该系列已经完结,若有不明白的地方可以私信咨询我~ 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您好,您可以通过以下步骤将 Python 2 升级Python 3: 1. 首先,确保您的操作系统上已经安装了 Python 3。 2. 然后,使用 pip 工具安装 2to3 工具,该工具可以将 Python 2 代码转换Python 3 代码。您可以使用以下命令安装: pip install 2to3 3. 接下来,使用 2to3 工具将您的 Python 2 代码转换Python 3 代码。您可以使用以下命令: 2to3 your_python2_code.py 4. 最后,使用 Python 3 运行转换后的代码,确保它可以正常工作。 希望这可以帮助您升级Python 3。 ### 回答2: 在升级Python 2到Python 3时,需执行以下步骤: 1. 检查系统中是否已经安装了Python 3版本。如未安装,可到Python官方网站下载最新的Python 3版本并进行安装。 2. 检查当前系统中Python 2的版本号,可通过在命令行输入`python -V`或`python --version`来查看当前Python版本。确认版本号后,还可以使用`python2 -V`或`python2 --version`来获取Python 2的版本号。 3. 执行Python 2到Python 3的转换脚本。针对不同的操作系统,可以选择使用2to3工具或futurize工具执行这一转换过程。这些工具可以将Python 2的代码自动转换成兼容Python 3的代码。此外,还可以通过手动修改代码的方式,逐步更改不兼容的语法或API。注意,某些特定的Python 2代码在转换时可能需要手动调整。 4. 进行代码测试和修复。在转换完成后,建议对代码进行全面的测试,以确保Python 3运行环境下的完全兼容性。可能需要修复一些兼容性问题或错误。使用工具如unittest对代码进行测试是一个不错的选择。 5. 更新依赖项和库。升级Python 3后,某些依赖项和库可能需要更新才能保持兼容性。在Python 3环境中安装最新版本的依赖项和库是一个很好的实践。 6. 迁移完整项目。一旦你完成了对Python 2代码的转换和测试,可以开始将整个项目从Python 2迁移到Python 3。确保在迁移过程中备份代码和数据以防万一。 需要注意的是,升级Python 2到Python 3可能需要投入大量的时间和资源,特别是对于复杂或庞大的项目。强烈建议在进行升级前评估工作量和风险,并选择合适的策略和工具来进行迁移。 ### 回答3: 要将Python 2升级Python 3,可以采取以下步骤: 1. 确认当前Python版本:打开终端或命令提示符,输入以下命令来检查Python版本:python --version。如果显示的版本号是2.x.x,那么你当前使用的是Python 2。 2. 下载Python 3:从Python官方网站(https://www.python.org/downloads/)下载适合你操作系统的Python 3版本,选择最新的稳定版本进行下载。 3. 安装Python 3:运行下载的安装包,按照安装向导的提示进行安装。 4. 更新路径变量(仅适用于Windows用户):打开控制面板,点击“系统和安全”或“系统”,然后点击“高级系统设置”。在弹出的窗口中,点击“环境变量”,然后在“系统变量”中找到名为“Path”的变量,选择编辑并在变量值字段中添加Python 3的安装路径。 5. 检查Python 3安装:在终端或命令提示符中输入以下命令:python3 --version。如果显示的版本号是3.x.x,那么说明Python 3已经成功安装。 请注意,升级Python 3可能会导致一些已经存在的Python 2代码不兼容。因此,在升级之前,需要仔细检查并修改现有的代码,以确保其与Python 3兼容。 另外,为了更好地适应Python 3,可以阅读Python 3的官方文档,并了解其新特性和改变。还可以参考一些迁移指南和教程,以帮助你平稳地过渡到Python 3。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值