Nameerror: Global Name 'Xrange' Is Not Defined in Python 3

NameError: global name 'xrange' is not defined in 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.

NameError: name 'xrange' is not defined when running tensorflow object detection api

xrange() was removed from python3 and was replaced by range(). Use range() instead (it works exactly the same as xrange()).

However, if you need to use the range() function in python2 (which you don't in this case), use xrange() as range() returns a list instead of a generator (as @xdurch0 says in the comments).

I am using Python 3 notebook but I am getting the following errors when I run it

xrange is not present in python3. Use range instead.

Why xrange is not defined when I'm not using xrange in the first place?

You are running stale bytecode, restart Python.

Python compiles source code to bytecode, and interprets the latter. This means the interpreter doesn't work with the source code once compiled.

However, us humans can't read bytecode very well, so when there is an exception and the interpreter wants us to understand where things went wrong, it would like to show you the source code again. Thus the source code is loaded on demand when there is a traceback to be shown, and lines are taken from the source code based on information recorded with the bytecode.

In your case, you are running bytecode that uses the name xrange. But you have already corrected the source code to use range instead. The bytecode throws an exception, and Python helpfully loads the source code from disk, and shows the already corrected sourcecode.

The solution is to tell Python to re-compile the source code, by restarting. If restarting doesn't help, then Python has determined that the source code is older than the bytecode it has cached. Delete the __pycache__ directory next to your source code to clear the bytecode cache, and remove any .pyc file that might be sitting in the same directory as your source.

Note that you can drop the list() call; you don't have to have a list for the for loop to work; for j in range(epoch): works just fine.

How to avoid error: name 'score history' is not defined in modules section in Python for absolute beginners by Mark Winerbottom

I think you just have a typo in your code "histrory" and not "history":

def get_avg(score_history):
"""Takes a list of previous grades and returns average."""
return sum(score_history) / len(score_history)


Related Topics



Leave a reply



Submit