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

6

395

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?

Cowans answered 19/6, 2013 at 13:13 Comment(0)
L
735

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.

Lanitalank answered 19/6, 2013 at 13:14 Comment(3)
Thanks for noting the difference between syntax error and runtime exception. Learned something new!Pernod
Well, this might be a good solution for compatibility but xrange was designed with the idea of memory efficiency. Just try working with millions length range objects instead of xrange objects.Dwarfish
@gabrielgarcia and that’s why the code gives you xrange in Python 2 and range in Python 3. Those are the memory efficient virtual sequences for those versions of Python.Lanitalank
T
29

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

Tannenbaum answered 7/2, 2018 at 7:15 Comment(2)
As 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.Libretto
@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.Staciestack
K
24

I solved the issue by adding this import
More info

from past.builtins import xrange
Karaganda answered 13/5, 2019 at 15:48 Comment(2)
--------------------------------------------------------------------------- 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'Milli
@Milli You have to go back to the future! pip3 install futurePopele
M
12

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.

Myrmecology answered 7/8, 2019 at 8:1 Comment(0)
E
5

Replace

Python 2 xrange to

Python 3 range

Rest all same.

Excurved answered 20/11, 2018 at 20:39 Comment(0)
O
0

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.

Ogdon answered 24/11, 2017 at 9:30 Comment(2)
This package to be used to get features of the newer version to the older version of python. Not other way around.Saltern
What past.builtings.range does is simply set xrange to be a reference to range. 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.Lanitalank

© 2022 - 2024 — McMap. All rights reserved.