title: How to execute code in the Django shell by an external python script?
body: <p><strong>What I want to achieve:</strong></p>
  
  <p>I would like to create a python script to deactivate Django users in the database from the CLI. I came up with this:</p>
  
  <pre><code>$ sudo python manage.py shell
  &gt;&gt;&gt; user = User.objects.get(username=FooBar)
  &gt;&gt;&gt; user.is_active = False
  &gt;&gt;&gt; user.save()
  &gt;&gt;&gt; exit()
  </code></pre>
  
  <p>The above code WORKS when I manually enter it manualy command after command. However, I would like to put execute the commands in one .py script like</p>
  
  <pre><code>$ sudo python script.py
  </code></pre>
  
  <p><strong>Now I've tried diffirent aproaches:</strong></p>
  
  <ul>
  <li><code>os.system("command1 &amp;&amp; command2 &amp;&amp; command3")</code></li>
  <li><code>subprocess.Popen("command1 &amp;&amp; command2 &amp;&amp; command3",
  stdout=subprocess.PIPE, shell=True)</code></li>
  </ul>
  
  <p><strong>The problem:</strong></p>
  
  <p>This does not work! <em>I think</em> this problem is here because Python waits until the opened Django shell (first command) finishes which is never. It doesn't execute the rest of the commands in the script as the first command puts it in Hold.</p>
  
  <p><code>subprocess.popen</code> can execute commands in a shell but only in the Python shell, I would like to use the Django shell.</p>
  
  <p>Anyone ideas how to access the Django shell with a .py script for custom code execution?</p>