5

I was getting this error when testing out a project I just set up

Exception Type: ImproperlyConfigured
Exception Value:    
This query requires pytz, but it isn't installed.

After a little googling I found that I was getting this because in my settings I specified

USE_TZ = True

which I believe is supposed to make my project timezone aware.

So I ran a sudo pip install pytz to install pytz so I wouldn't get this error. The problem is that now , with pytz installed my models won't validate when I try to do ./manage.py runserver and I get this error

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
    from django.utils.log import configure_logging
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/utils/log.py", line 10, in <module>
    from django.views.debug import ExceptionReporter, get_exception_reporter_filter
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/views/debug.py", line 10, in <module>
    from django.http import (HttpResponse, HttpResponseServerError,
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/http/__init__.py", line 4, in <module>
    from django.http.response import (HttpResponse, StreamingHttpResponse,
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/http/response.py", line 13, in <module>
    from django.core.serializers.json import DjangoJSONEncoder
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/core/serializers/__init__.py", line 23, in <module>
    from django.core.serializers.base import SerializerDoesNotExist
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/core/serializers/base.py", line 6, in <module>
    from django.db import models
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/db/models/__init__.py", line 6, in <module>
    from django.db.models.query import Q, QuerySet, Prefetch  # NOQA
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/db/models/query.py", line 13, in <module>
    from django.db.models.fields import AutoField, Empty
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 15, in <module>
    from django.db.models.lookups import default_lookups, RegisterLookupMixin
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/db/models/lookups.py", line 6, in <module>
    from django.utils import timezone
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/utils/timezone.py", line 149, in <module>
    utc = pytz.utc if pytz else UTC()
AttributeError: 'module' object has no attribute 'utc'

What is happening here? Why did I get this error? To me this looks like the problem is coming from pytz but I am not sure. Any help would be greatly appreciated.

Here is my model, which worked fine before installing pytz

from django.db import models
from django.conf import settings

# Create your models here.
class Item(models.Model):
    title = models.CharField(max_length=100, )
    description = models.TextField()
    seller = models.ForeignKey(settings.AUTH_USER_MODEL)
    price = models.DecimalField(max_digits=11, decimal_places=2)
    timestamp = models.DateTimeField(auto_now_add=True)
    last_updated = models.DateTimeField(auto_now=True)


    def __str__(self):
        return self.title

Edit: I added my views.py file

from .models import Item

from django.views.generic import ListView, DetailView

class ItemListView(ListView):
    model = Item

class ItemDetailView(DetailView):
    model = Item

    def get_context_data(self, **kwargs):
       # Call the base implementation first to get a context
       #This is getting a dictionary of the objects being displayed to
       #the template
       context = super(ItemDetailView, self).get_context_data(**kwargs)
       # Add in a QuerySet of all items up for trade by the user
       context['other_things_for_trade_by_user'] = Item.objects.filter(seller=context['seller'])
       return context
guribe94
  • 1,551
  • 3
  • 15
  • 29
  • check `TIME_ZONE` in your settings and try `from django.utils import timezone` change in your fields `models.DateTimeField(default=timezone.now)`, here in docs https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#naive-and-aware-datetime-objects – madzohan Nov 03 '14 at 09:36
  • I tried changing it to that and I am still getting the same error `AttributeError: 'module' object has no attribute 'utc'` – guribe94 Nov 03 '14 at 19:52
  • show your query, that requires pytz – madzohan Nov 03 '14 at 19:56
  • I don't know if I have one. Until this happened I didn't know pytz existed. What would that be? – guribe94 Nov 03 '14 at 19:58
  • queries in views that operate and render **Item** objects – madzohan Nov 03 '14 at 20:01
  • 1
    try test **pytz** http://stackoverflow.com/a/15692958/3033586 in a shell – madzohan Nov 03 '14 at 20:06
  • I added my views.py file I believe thats the only place I operate and render item objects. Do I also do this in forms.py? – guribe94 Nov 03 '14 at 20:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64195/discussion-between-madzohan-and-guribe94). – madzohan Nov 03 '14 at 20:10
  • Thank you very much but I was able to resolve the issue by just reinstalling pytz. I found that the pytz test that you showed me did not work as intended – guribe94 Nov 05 '14 at 22:24
  • @guribe94 I have the same issue right now, how did you reinstall pytz? What source? This is really mindnumbing. – imrek Aug 01 '15 at 21:42
  • Can you give pytz and django's versions ? – Zulu Aug 03 '15 at 00:07

1 Answers1

2

As suggested this issue was caused by mismatching versions of pytz and django. This was fixed by uninstalling pytz and django and reinstalling them both with pip.

guribe94
  • 1,551
  • 3
  • 15
  • 29