NameError:全局名称“xrange”未在 Python 3 中定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17192158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NameError: global name 'xrange' is not defined in Python 3
提问by Pip
I am getting an error when running a python program:
运行python程序时出现错误:
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 110, in <module>
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 27, in __init__
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\class\inventory.py", line 17, in __init__
builtins.NameError: global name 'xrange' is not defined
The game is from here.
游戏从这里开始。
What causes this error?
是什么导致了这个错误?
采纳答案by Martijn Pieters
You are trying to run a Python 2 codebase with Python 3. xrange()was renamed to range()in Python 3.
您正在尝试使用 Python 3 运行 Python 2 代码库。在 Python 3xrange()中重命名为range()。
Run the game with Python 2 instead. Don't try to port it unless you know what you are doing, most likely there will be more problems beyond xrange()vs. range().
改为使用 Python 2 运行游戏。除非你知道你在做什么,否则不要尝试移植它,很可能会有更多的问题超越xrange()vs. range().
For the record, what you are seeing is not a syntax error but a runtime exception instead.
作为记录,您看到的不是语法错误,而是运行时异常。
If you do know what your are doing and are actively making a Python 2 codebase compatible with Python 3, you can bridge the code by adding the global name to your module as an alias for range. (Take into account that you mayhave to update any existing range()use in the Python 2 codebase with list(range(...))to ensure you still get a list object in Python 3):
如果您确实知道自己在做什么并且正在积极使 Python 2 代码库与 Python 3 兼容,则可以通过将全局名称作为range. (考虑到您可能需要更新range()Python 2 代码库中的任何现有用法,list(range(...))以确保您仍然获得 Python 3 中的列表对象):
try:
# Python 2
xrange
except NameError:
# Python 3, xrange is now named range
xrange = range
# Python 2 code that uses xrange(...) unchanged, and any
# range(...) replaced with list(range(...))
or replace all uses of xrange(...)with range(...)in the codebase and then use a different shim to make the Python 3 syntax compatible with Python 2:
或者在代码库中替换所有使用xrange(...)with range(...),然后使用不同的 shim 使 Python 3 语法与 Python 2 兼容:
try:
# Python 2 forward compatibility
range = xrange
except NameError:
pass
# Python 2 code transformed from range(...) -> list(range(...)) and
# xrange(...) -> range(...).
The latter is preferable for codebases that want to aim to be Python 3 compatible onlyin the long run, it is easier to then just use Python 3 syntax whenever possible.
后者更适合希望仅从长远来看与 Python 3 兼容的代码库,然后尽可能使用 Python 3 语法更容易。
回答by Not Genius
I agree with the last answer.But there is another way to solve this problem.You can download the package named future,such as pip install future.And in your .py file input this "from past.builtins import xrange".This method is for the situation that there are many xranges in your file.
我同意最后一个答案。但是还有另一种方法可以解决这个问题。您可以下载名为 future 的包,例如 pip install future。并在您的 .py 文件中输入此“from past.builtins import xrange”。此方法适用于文件中有很多 xrange 的情况。
回答by Frost Xu
add xrange=rangein your code :) It works to me.
添加xrange=range您的代码:)它对我有用。
回答by Harry_pb
Replace
代替
Python 2 xrangeto
Python 2xrange到
Python 3 range
蟒蛇 3 range
Rest all same.
休息都一样。
回答by Andrés Gutiérrez
回答by Ahmad Farhan
in python 2.x, xrangeis used to return a generator while rangeis used to return a list. In python 3.x , xrangehas been removed and rangereturns a generator just like xrangein python 2.x. Therefore, in python 3.x you need to use range rather than xrange.
在 python 2.x 中,xrange用于返回生成器,而range用于返回列表。在 python 3.x 中,xrange已被删除并且range返回一个生成器,就像python 2.x 中的xrange一样。因此,在 python 3.x 中,您需要使用 range 而不是 xrange。

