391

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?

smci
  • 32,567
  • 20
  • 113
  • 146
Pip
  • 4,387
  • 4
  • 23
  • 31

6 Answers6

730

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 9
    Thanks for noting the difference between syntax error and runtime exception. Learned something new! – Pithikos Nov 04 '14 at 10:30
  • 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. – gabriel garcia Jun 12 '21 at 18:13
  • @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. – Martijn Pieters Jun 12 '21 at 21:36
29

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

Frost Xu
  • 315
  • 3
  • 2
  • 9
    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. – RobinFrcd Feb 07 '18 at 07:21
  • 2
    @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. – ZF007 Feb 07 '18 at 07:38
23

I solved the issue by adding this import
More info

from past.builtins import xrange
  • 2
    --------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) in () ----> 1 from past.builtins import xrange 2 for i in xrange(10): 3 print(i) ModuleNotFoundError: No module named 'past' – rsc05 Oct 23 '19 at 11:44
  • @rsc05 You have to go back to the future! `pip3 install future` – SurpriseDog Apr 29 '21 at 15:03
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.

Ahmad Farhan
  • 575
  • 4
  • 12
4

Replace

Python 2 xrange to

Python 3 range

Rest all same.

Hari_pb
  • 7,088
  • 3
  • 45
  • 53
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.

Not Genius
  • 37
  • 1
  • 1
  • 3
  • This package to be used to get features of the newer version to the older version of python. Not other way around. – mcsim Nov 24 '17 at 09:57
  • 1
    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. – Martijn Pieters Jan 21 '18 at 12:49