Skip to content

NameError: global name 'xrange' is not defined in Python 3 - and solution proposal #14725

Closed

Description

@guyko81

System information

You can collect some of this information using our environment capture script:

https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh

You can obtain the TensorFlow version with

python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"

Describe the problem

Running the example code under python 3.6 in Docker environment I got xrange error: NameError: global name 'xrange' is not defined in Python 3

I propose to change every xrange to range under the 3.x versions:

patch for xrange

find /opt/conda/lib/python3.6/site-packages/tensorflow/contrib/boosted_trees -type f -print0 | xargs -0 sed -i 's/ xrange(/ range(/g'

Source code / logs

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

Activity

  1. mrry commented on Nov 20, 2017

    @mrry
    Contributor

    I think we use from six.moves import xrange elsewhere in the codebase to handle this incompatibility, without the potential for a performance regression on Python 2.7. Any file that uses xrange() should have this import at the top.

  2. guyko81 commented on Nov 20, 2017

    @guyko81
    Author

    It didn't work for me, that's why I used this workaround.

  3. sshrdp commented on Nov 20, 2017

    @sshrdp
    Member

    This issue has already been fixed (cc2030d) but unfortunately didn't make it to 1.4.

  4. guyko81 commented on Nov 21, 2017

    @guyko81
    Author

    oh, thank you then :)

  5. neel17 commented on Dec 19, 2018

    @neel17

    You can try to change the code line 44 in
    for i in xrange(self.num_pages):
    change it to
    for i in list(range(self.num_pages)):

    I hope this should work and kindly restart your python before running the code.

  6. SUNILARAVA commented on Jan 25, 2020

    @SUNILARAVA

    I think we use from six.moves import xrange elsewhere in the codebase to handle this incompatibility, without the potential for a performance regression on Python 2.7. Any file that uses xrange() should have this import at the top.

    Thanks it worked for Python 2.7

  7. ManuelNevado commented on Dec 17, 2020

    @ManuelNevado

    I think we use from six.moves import xrange elsewhere in the codebase to handle this incompatibility, without the potential for a performance regression on Python 2.7. Any file that uses xrange() should have this import at the top.

    Thanks it worked for me :)

  8. added 3 commits that reference this issue on Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions