NameError: global name 'xrange' is not defined in Python 3
Solution 1
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.
Solution 2
add xrange=range
in your code :) It works to me.
Solution 3
I solved the issue by adding this import
More info
from past.builtins import xrange
Solution 4
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.
Solution 5
Replace
Python 2 xrange
to
Python 3 range
Rest all same.
Pip
Hey! I'm a hobby game developer, currently working on an experimental C# game framework called Flare, which aims to automate the tasks that I and others commonly do for every project, such as opening a window or getting input in a platform-independent way. Flare provides OpenGL 3+ core bindings, but not compatibility bindings so supports OpenGL 3.0 and upwards, with the default context being 3.2 core. It also provides an optional XNA-like sprite rendering system as well as a 2D text rendering system. I have experience with programming languages including C#, C/C++, Ruby, Java, and Python, and a passing acquaintance with several others. I also have experience with technologies including XNA/MonoGame, Unity, Ruby on Rails, FRC/FTC robotics libraries (WPILib and the FTC SDK), as well as the afore-mentioned OpenGL and some mobile development. I currently attend high school in the United States. Links Github: WardBenjamin Twitter: TheProgramm3r Website: Benjamin Ward
Updated on February 12, 2020Comments
-
Pip over 4 years
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?
-
Pithikos over 9 yearsThanks for noting the difference between syntax error and runtime exception. Learned something new!
-
mcsim over 6 yearsThis package to be used to get features of the newer version to the older version of python. Not other way around.
-
Martijn Pieters over 6 yearsWhat
past.builtings.range
does is simply setxrange
to be a reference torange
. This is helpful when creating a Python 2 / 3 polyglot codebase, but not suitable for an existing project that's designed to work on Python 2 only. -
RobinFrcd over 6 yearsAs it's said in the accepted answer, don't do that, it's likely there will be other issues. Just run that code in python 2.
-
ZF007 over 6 years@Frost Xu ... please consider Robin Fourcade his comment and remove your answer. Its an IDE for beginners and you don't want to start with diving deep into debugging and solely learn about python 2/3 changes. Kinda waist of your time.
-
rsc05 over 4 years--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) <ipython-input-21-bcd3600b3604> in <module>() ----> 1 from past.builtins import xrange 2 for i in xrange(10): 3 print(i) ModuleNotFoundError: No module named 'past'
-
SurpriseDog about 3 years@rsc05 You have to go back to the future!
pip3 install future
-
gabriel garcia about 3 yearsWell, this might be a good solution for compatibility but
xrange
was designed with the idea ofmemory efficiency
. Just try working with millions lengthrange
objects instead ofxrange
objects. -
Martijn Pieters about 3 years@gabrielgarcia and that’s why the code gives you
xrange
in Python 2 andrange
in Python 3. Those are the memory efficient virtual sequences for those versions of Python.