ImportError : cannot import name timezone pythonanywhere
<p>I am attempting to use collectstatic in the bash console to get my CSS running on a django app on pythonanywhere.</p>

<p>Unfortunately, I am getting an error:</p>

<pre><code>23:49 ~/mysite/mysite $ python manage.py collectstatic                                                                                                                                                            
Traceback (most recent call last):
  File "manage.py", line 22, in &lt;module&gt;
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 429, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 219, in execute
    self.validate()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 35, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 146, in get_app_errors
    self._populate()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 64, in _populate
    self.load_app(app_name)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 78, in load_app
    models = import_module('.models', app_name)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/nikk2009/mysite/mysite/polls/models.py", line 4, in &lt;module&gt;
    from django.utils import timezone
ImportError: cannot import name timezone
23:49 ~/mysite/mysite $ 
</code></pre>

<p>Here is the .py where timezone is imported </p>

<pre><code>import datetime

from django.db import models
from django.utils import timezone

# Create your models here.

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        now = timezone.now()
        return self.pub_date &gt;= timezone.now() - datetime.timedelta(days=1)&lt;= now
        was_published_recently.admin_order_field = 'pub_date'
        was_published_recently.boolean = True
        was_published_recently.short_description = 'Published recently?'

class Choice(models.Model):
    choice_text = models.CharField(max_length= 200)
    votes = models.IntegerField(default= 0)
    question = models.ForeignKey(Question, on_delete=models.CASCADE)

    def __str__(self):
        return self.choice_text
</code></pre>

<p>If I am not mistaken, pythonanywhere uses Django 1.3.7 by default. It looks like Django timezone support was not added until version 1.4:</p>

<p><a href="https://docs.djangoproject.com/en/1.10/releases/1.4/#what-s-new-in-django-1-4" rel="nofollow">https://docs.djangoproject.com/en/1.10/releases/1.4/#what-s-new-in-django-1-4</a></p>

<p>You should update Django to the most recent version (or at least an earlier version) and everything should work as expected (at least with timezones). You can upgrade by opening a bash console from the <strong>Consoles</strong> tab on your pythonanywhere profile, and running the command: </p>

<pre><code>$ pip install --upgrade django
</code></pre>

<p>Or install a newer version in a <code>virtualenv</code>:</p>

<pre><code>$ mkvirtualenv myenv --python=/usr/bin/python3.4

$ pip install django
</code></pre>

<h1>Edit:</h1>

<p>I tested out my first suggestion, and was not able to get it to work on my pythonanywhere account (I think it has to do with the permissions that pythonanywhere gives their users). However, using the second method (i.e., using a <code>virtualenv</code>) did work to install the latest version of Django, which does include timezone support in <code>django.utils.timezone</code>.</p>

python css django importerror pythonanywhere