Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: global name 'xrange' is not defined in Python 3

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?

like image 662
Pip Avatar asked Jun 19 '13 13:06

Pip


People also ask

Is Xrange available in Python 3?

In Python 3, there is no xrange, but the range function behaves like xrange in Python 2. If you want to write code that will run on both Python 2 and Python 3, you should use range().

Why Xrange is not defined?

The Python "NameError: name 'xrange' is not defined" when we use the xrange() function in a Python 3 codebase. To solve the error, use the range() function instead, because xrange was renamed to range in Python 3. Here is an example of how the error occurs.

What is Xrange and range in Python?

range() – This returns a range object (a type of iterable). xrange() – This function returns the generator object that can be used to display numbers only by looping. The only particular range is displayed on demand and hence called “lazy evaluation“.


2 Answers

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.

like image 114
Martijn Pieters Avatar answered Sep 28 '22 06:09

Martijn Pieters


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

like image 25
Frost Xu Avatar answered Sep 28 '22 04:09

Frost Xu