title: Python import problem with Django management commands
body: <p>For whatever reason, when I was new to Python and Django, I wrote some import statements like this at the top of a models.py file:</p>
  
  <pre><code>from django.contrib import auth
  </code></pre>
  
  <p>And I'd use it like this:</p>
  
  <pre><code>class MyModel(models.Model):
      user = models.ForeignKey(auth.models.User)
      # ...
  </code></pre>
  
  <p>This worked fine. A long time later, I wrote a custom management command, and it would do this:</p>
  
  <pre><code>from myapp.models import MyModel
  </code></pre>
  
  <p>When I ran my custom command (<code>python manage.py my_command</code>) this would result in Python complaining that the module <code>auth</code> had no attribute <code>models</code> on the line declaring the <code>ForeignKey</code> in models.py.</p>
  
  <p>To work around this problem, I changed my models.py to the more usual:</p>
  
  <pre><code>from django.contrib.auth.models import User
  
  class MyModel(models.Model):
      user = models.ForeignKey(User)
      # ...
  </code></pre>
  
  <p>Can someone explain to me what I am missing? Is there something different in the environment when you run a management command? Or was I just doing it wrong the whole time? Thanks!</p>
  
  <p><strong>Edit</strong>: Following dmitko's hunch about circular imports, here are the imports used in my models.py file. I'm showing the original import of <code>auth</code> commented out, along with the only model that has a foreign key to the auth user model:</p>
  
  <pre><code>import datetime  
  from django.db import models 
  # from django.contrib import auth
  from django.contrib.auth.models import User 
  
  class UserLastVisit(models.Model):
      # user = models.ForeignKey(auth.models.User, unique=True)
      #                          ^^^^^^^^^^^^^^^^
      # after adding mgmt command, error occurred here; change to the line below
      user = models.ForeignKey(User, unique=True)
      last_visit = models.DateTimeField(db_index=True)
  </code></pre>
  
  <p>And here are the imports of the management command that uncovered the problem:</p>
  
  <pre><code>import datetime   
  from django.core.management.base import NoArgsCommand 
  from core.models import UserLastVisit, AnonLastVisit, Statistic
  </code></pre>
  
  <p>Was this setting up a circular import type situation?</p>