NameError:全局名称“ xrange”未在Python 3中定义

问题:NameError:全局名称“ xrange”未在Python 3中定义

运行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

游戏是从这里开始的

是什么导致此错误?

I am getting an error when running a python program:

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?


回答 0

您正在尝试使用Python 3运行Python 2代码库。在Python 3中xrange()已重命名为range()

而是使用Python 2运行游戏。不要试图将它移植,除非你知道自己在做什么,很可能会出现超越更多的问题xrange()range()

作为记录,您看到的不是语法错误,而是运行时异常。


如果您确实知道自己在做什么,并且正在积极地使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(...))

或更换的所有用途xrange(...)range(...)在代码库,然后使用不同的垫片,使与Python 2的Python语法3兼容:

try:
    # Python 2 forward compatibility
    range = xrange
except NameError:
    pass

# Python 2 code transformed from range(...) -> list(range(...)) and
# xrange(...) -> range(...).

对于希望长期与Python 3兼容的代码库而言,后者是更可取的,因此只要有可能,便更容易使用Python 3语法。

You are trying to run a Python 2 codebase with Python 3. xrange() was renamed to range() in Python 3.

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().

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 may have 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):

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:

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 only in the long run, it is easier to then just use Python 3 syntax whenever possible.


回答 1

添加xrange=range您的代码:)对我有用。

add xrange=range in your code :) It works to me.


回答 2

我加入这个解决进口问题的
更多信息

from past.builtins import xrange

I solved the issue by adding this import
More info

from past.builtins import xrange

回答 3

在python 2.x中,xrange用于返回生成器,而range用于返回列表。在python 3.x中,xrange已被删除,并且range返回一个生成器,就像python 2.x中的xrange一样。因此,在python 3.x中,您需要使用range而不是xrange。

in python 2.x, xrange is used to return a generator while range is used to return a list. In python 3.x , xrange has been removed and range returns a generator just like xrange in python 2.x. Therefore, in python 3.x you need to use range rather than xrange.


回答 4

更换

Python 2 xrange

Python 3 range

休息都一样。

Replace

Python 2 xrange to

Python 3 range

Rest all same.


回答 5

我同意最后一个答案。但是还有另一种方法可以解决此问题。您可以下载名为future的软件包,例如pip install future。然后在.py文件中输入“ from past.builtins import xrange”。此方法用于文件中有很多xrange的情况。

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.