How to detect if script ran from Django or command prompt?
<p>I have a Python script that pauses for user input (using <code>raw_input</code>, recently I created a Django web UI for this script. Now when I execute the script via Django is pauses as it's waiting for input in the backend. </p>

<p>How can I determine if the script was ran from Django or terminal/cmd/etc? I don't want to maintain 2 streams of code, one for web and another one for terminal.</p>

<p>Why not use <code>__main__</code>: <a href="https://docs.python.org/3/library/__main__.html" rel="nofollow">https://docs.python.org/3/library/<strong>main</strong>.html</a></p>

<pre><code>if __name__ == '__main__':
    print ('running as a script')
else:
    print ('running as a web app')
</code></pre>

<p>Works on both python 2.7 and 3.x
Also see: <a class='doc-link' href="http://stackoverflow.com/documentation/django/5848/django-from-the-command-line/20596/django-from-the-command-line#t=201610040433419117038">Django from the command line.</a></p>

python django