from django.utils.six.moves import range, ImportError: cannot import name 'range'

334 views
Skip to first unread message

Derek Zeng

unread,
Sep 2, 2017, 11:17:37 PM9/2/17
to Django users
Hi,

Today I tried to install my django project on a new mac computer. I used virutalenv to setup the packages. 

After I start the app by running './mange.py runserver' and access the home page I got the error 

from django.utils.six.moves import range, ImportError: cannot import name 'range'

Really not sure why it has this weird error. I'm using python 3.6.2 

Any help is appreciated!

James Schneider

unread,
Sep 3, 2017, 12:11:03 AM9/3/17
to django...@googlegroups.com
Is the referenced error coming from code that you wrote?

There's a comma at the end of the import line, which may cause that error. 

Also make sure that you've installed Django within your virtualenv. If it is, then also make sure you have six installed (although Django should have done that already).

-James

Derek Zeng

unread,
Sep 3, 2017, 10:26:33 AM9/3/17
to Django users
It is a project I developed in ubuntu and currently running on a ubuntu server. I just bought a new mac mini, so I would like to develop on mac.

I have isolated the problem to the django version.
 
When I use django==1.10.7, this does not happen.
When I use django==1.11a1 this happens. 

This is the full stack trace:

Traceback (most recent call last):
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 157, in __call__
    response = self.get_response(request)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in get_response
    response = self._middleware_chain(request)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/core/handlers/exception.py", line 43, in inner
    response = response_for_exception(request, exc)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/core/handlers/exception.py", line 93, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/core/handlers/exception.py", line 139, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/six.py", line 686, in reraise
    raise value
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/coderek/Documents/projects/mysite/mysite/urls.py", line 78, in home
    return render(req, 'index.html', {'posts': posts})
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/shortcuts.py", line 30, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/template/loader.py", line 68, in render_to_string
    return template.render(context, request)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/template/backends/django.py", line 66, in render
    return self.template.render(context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/template/base.py", line 207, in render
    return self._render(context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/template/loader_tags.py", line 177, in render
    return compiled_parent._render(context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/compressor/templatetags/compress.py", line 131, in render
    return self.render_compressed(context, self.kind, self.mode, forced=forced)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/compressor/templatetags/compress.py", line 98, in render_compressed
    compressor = self.get_compressor(context, kind)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/compressor/templatetags/compress.py", line 39, in get_compressor
    content=self.get_original_content(context), context=context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/compressor/templatetags/compress.py", line 123, in get_original_content
    return self.nodelist.render(context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/templatetags/static.py", line 105, in render
    url = self.url(context)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/templatetags/static.py", line 102, in url
    return self.handle_simple(path)
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/templatetags/static.py", line 116, in handle_simple
    from django.contrib.staticfiles.storage import staticfiles_storage
  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 21, in <module>
    from django.utils.six.moves import range
ImportError: cannot import name 'range'

James Schneider

unread,
Sep 3, 2017, 2:55:50 PM9/3/17
to django...@googlegroups.com


  File "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 21, in <module>
    from django.utils.six.moves import range
ImportError: cannot import name 'range'

Are you sure you're using 3.6.2? Do you have another virtualenv with 3.5 and no Django installed?

-James

Derek Zeng

unread,
Sep 3, 2017, 5:54:17 PM9/3/17
to django...@googlegroups.com
it's the same in either versions of python. the output was captured when i was just debugging it in 3.5. 

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/49blpAjNxYc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciV4vpt7Vk%3D8GDS_wNQHqhsuwA5vcfakWo%3DoVjQziXLdxA%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Melvyn Sopacua

unread,
Sep 4, 2017, 2:18:25 AM9/4/17
to django...@googlegroups.com
Could you turn off DEBUG and check what the problem is then?
The reason is that I want to make sure we're not judging the side
effect of *rendering* the exception.

On Sun, Sep 3, 2017 at 11:52 PM, Derek Zeng <zen...@gmail.com> wrote:
> it's the same in either versions of python. the output was captured when i
> was just debugging it in 3.5.
>
> On Sun, Sep 3, 2017 at 2:55 PM, James Schneider <jrschn...@gmail.com>
> wrote:
>>
>>
>>
>> File
>> "/Users/coderek/Documents/projects/py3.5/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py",
>> line 21, in <module>
>> from django.utils.six.moves import range
>> ImportError: cannot import name 'range'
>>
>>
>> Are you sure you're using 3.6.2? Do you have another virtualenv with 3.5
>> and no Django installed?
>>
>> -James
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Django users" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/django-users/49blpAjNxYc/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> django-users...@googlegroups.com.
>> To post to this group, send email to django...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CA%2Be%2BciV4vpt7Vk%3D8GDS_wNQHqhsuwA5vcfakWo%3DoVjQziXLdxA%40mail.gmail.com.
>>
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAAzyz%2BG0MTUNd8xm8ajNby38eoi7Xmj6RgXBZwp2WfAS-n9s2g%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Melvyn Sopacua

Raffaele Salmaso

unread,
Sep 4, 2017, 2:34:27 AM9/4/17
to django...@googlegroups.com
On Sun, Sep 3, 2017 at 4:26 PM, Derek Zeng <zen...@gmail.com> wrote:
When I use django==1.10.7, this does not happen.
When I use django==1.11a1 this happens. 
Why 1.11a1? Current is 1.11.4

--

Derek Zeng

unread,
Sep 4, 2017, 9:33:51 AM9/4/17
to django...@googlegroups.com
If I turn off debug mode, it gives me 500 error and showing the same stack trace in the console.

I also tried 1.11.4, still the same error.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/49blpAjNxYc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Melvyn Sopacua

unread,
Sep 4, 2017, 12:38:08 PM9/4/17
to django...@googlegroups.com
Rename MIDDLEWARE_CLASSES to
MIDDLEWARE in settings to see if that changes anything.
If not, remove one by one (except static). There are some changes to
the static files middleware [1], but I can't really relate that to
your error.

Is there anything in your project requirements that also uses six?

There's no diff between 1.10.x and 1.11.x on six.py, but this code
looks like a possible root cause:

# Here's some real nastiness: Another "instance" of the six module might
# be floating around. Therefore, we can't use isinstance() to check for
# the six meta path importer, since the other six instance will have
# inserted an importer with different class.
if (type(importer).__name__ == "_SixMetaPathImporter" and
importer.name == __name__):
del sys.meta_path[i]
break

[1] https://docs.djangoproject.com/en/1.11/releases/1.11/#django-contrib-staticfiles

On Mon, Sep 4, 2017 at 3:32 PM, Derek Zeng <zen...@gmail.com> wrote:
> If I turn off debug mode, it gives me 500 error and showing the same stack
> trace in the console.
>
> I also tried 1.11.4, still the same error.
>
> On Mon, Sep 4, 2017 at 2:33 AM, Raffaele Salmaso <raff...@salmaso.org>
> wrote:
>>
>> On Sun, Sep 3, 2017 at 4:26 PM, Derek Zeng <zen...@gmail.com> wrote:
>>>
>>> When I use django==1.10.7, this does not happen.
>>> When I use django==1.11a1 this happens.
>>
>> Why 1.11a1? Current is 1.11.4
>>
>> --
>> | Raffaele Salmaso
>> | https://salmaso.org
>> | https://bitbucket.org/rsalmaso
>> | https://github.com/rsalmaso
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Django users" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/django-users/49blpAjNxYc/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> django-users...@googlegroups.com.
>> To post to this group, send email to django...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CABgH4Js1z0e7yypGD4f7z_eKbMrYWQEMT9viCSCshA1ROhMA_A%40mail.gmail.com.
>>
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAAzyz%2BG2MjxRv001kjq%3DpkpzReVAHw03Jv9FW6Ry7sHPwKMsSQ%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Melvyn Sopacua

Derek Zeng

unread,
Sep 4, 2017, 3:04:31 PM9/4/17
to django...@googlegroups.com
Hi Melvyn,

Renaming MIDDELWARE_CLASSES doesn't help either. 

These are the requirements I have:

django==1.10.5
djangorestframework
markdown
django-filter
django-jinja
pytest
factory_boy

fabric3
pytest-django

feedparser==5.2.1
pytz
PyYAML
lxml
requests

django-extensions
Werkzeug

django-cors-headers
psycopg2
django_compressor
django-libsass
livereload

---

I tried to comment out the code you quoted. But that has no effects.



>> To post to this group, send email to django...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CABgH4Js1z0e7yypGD4f7z_eKbMrYWQEMT9viCSCshA1ROhMA_A%40mail.gmail.com.
>>
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an

> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/49blpAjNxYc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Melvyn Sopacua

unread,
Sep 4, 2017, 3:27:53 PM9/4/17
to django...@googlegroups.com
Is it possible you create your virtual environments
with global site-packages?
From what I'm seeing this is caused primarily by
version mismatches of the six library and using
OS site-packages with virtualenv would certainly
do it as setuptools and pip both use six.

The difference with 1.10/1.11 I cannot explain
though.
>> >> django-users...@googlegroups.com.
>> >> To post to this group, send email to django...@googlegroups.com.
>> >> Visit this group at https://groups.google.com/group/django-users.
>> >> To view this discussion on the web visit
>> >>
>> >> https://groups.google.com/d/msgid/django-users/CABgH4Js1z0e7yypGD4f7z_eKbMrYWQEMT9viCSCshA1ROhMA_A%40mail.gmail.com.
>> >>
>> >> For more options, visit https://groups.google.com/d/optout.
>> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Django users" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to django-users...@googlegroups.com.
>> > To post to this group, send email to django...@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/django-users.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/django-users/CAAzyz%2BG2MjxRv001kjq%3DpkpzReVAHw03Jv9FW6Ry7sHPwKMsSQ%40mail.gmail.com.
>> >
>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>> Melvyn Sopacua
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Django users" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/django-users/49blpAjNxYc/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> django-users...@googlegroups.com.
>> To post to this group, send email to django...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CA%2Bgw1GV3fMt5w4wUjOSviZzkdaHRU%2BFj4fHJqRSJnMGpcdLUNA%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAAzyz%2BEiQYvTZO41n9QsFiZ_%3DxgH3Hkrsu%2BZ6erX7CO74VKNEw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages