gpt4 book ai didi

python - 将 Django 项目从 Python 2 转换为 Python 3 : How to fix Python int OverFlowError?

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:54 24 4
gpt4 key购买 nike

我正在将 Django 网站从 Python 2 转换为 Python 3。为此,我对整个项目运行了 2to3。现在,在运行服务器(在 Python 2 中运行良好)时,会发生 OverflowError,如第一个代码块中所示。下面的 block 显示了 manage.py 文件。

我在别处读到这可能是与 int/float 相关的问题,但我不太确定如何处理与此相关的迭代器。

(env) user:languages user$ python3 manage.py runserver
Fatal Python error: initsite: Failed to import the site module
Traceback (most recent call last):
File ".../src/languages/env/bin/../lib/python3.7/site.py", line 66, in <module>
import os
File ".../src/languages/env/bin/../lib/python3.7/os.py", line 661, in <module>
from _collections_abc import MutableMapping
File "...src/languages/env/bin/../lib/python3.7/_collections_abc.py", line 45, in <module>
longrange_iterator = type(iter(list(range(1 << 1000))))
OverflowError: Python int too large to convert to C ssize_tappleperson
#!/usr/bin/env python3

import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "site.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)

我希望 manage.py 能够正常运行服务器并生成网站,但它却因上述溢出错误而挂起。

最佳答案

原始代码在_collections_abc.py ,在 Python 3 版本中,是:

longrange_iterator = type(iter(range(1 << 1000)))

你的版本是

longrange_iterator = type(iter(list(range(1 << 1000))))

看起来您也在 Python3 代码上使用了 2to3。 range 在 Python 2 中创建了一个列表,因此在 Python 3 中完全等价的是 list(range(...)),这似乎是发生的替换这里。它失败了,因为它强制代码创建一个巨大的列表,而原始列表没有。

因此,在使用 2to3 时,仅将其应用于 Python 2 代码。

关于python - 将 Django 项目从 Python 2 转换为 Python 3 : How to fix Python int OverFlowError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57884323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com