Django's runscript: No (valid) module for script 'filename' found
<p>I'm trying to run a script from the Django shell using the Django-extension <a href="http://django-extensions.readthedocs.io/en/latest/runscript.html" rel="nofollow">RunScript</a>. I have done this before and but it refuses to recognize my new script:</p>

<pre><code>(env) mint@mint-VirtualBox ~/GP/GP $ python manage.py runscript fill_in_random_variants
No (valid) module for script 'fill_in_random_variants' found
Try running with a higher verbosity level like: -v2 or -v3
</code></pre>

<p>While running any other script works fine:</p>

<pre><code>(env) mint@mint-VirtualBox ~/GP/GP $ python manage.py runscript fill_in_variants
Success! At least, there were no errors.
</code></pre>

<p>I have double checked that the file exists, including renaming it to something else. I have also tried running the command with non-existent script names:</p>

<pre><code>(env) mint@mint-VirtualBox ~/GP/GP $ python manage.py runscript thisfiledoesntexist
No (valid) module for script 'thisfiledoesntexist' found
Try running with a higher verbosity level like: -v2 or -v3
</code></pre>

<p>and the error is the same.</p>

<p>Why can't RunScript find my file?</p>

<p>RunScript has confusing error messages. It gives the same error for when it can't find a script at all and when there's an import error in the script.</p>

<p>Here's an example script to produce the error:</p>

<pre><code>import nonexistrentpackage

def run():
    print("Test")
</code></pre>

<p>The example has the only stated requirement for scripts, namely a <code>run</code> function.</p>

<p>Save this as <code>test_script.py</code> in a scripts folder (such as <code>project root/your app/scripts/test_script.py</code>). Then try to run it:</p>

<pre><code>(env) mint@mint-VirtualBox ~/GP/GP $ python manage.py runscript test_script
No (valid) module for script 'test_script' found
Try running with a higher verbosity level like: -v2 or -v3
</code></pre>

<p>Which is the same error as the file not found one. Now outcomment the import line and try again:</p>

<pre><code>(env) mint@mint-VirtualBox ~/GP/GP $ python manage.py runscript test_script
Test
</code></pre>

<p>As far as I know, the only way to tell the errors apart is to use the verbose (-v2) command line option and then look at the <em>first</em> (scroll up) error returned:</p>

<pre><code>(env) mint@mint-VirtualBox ~/GP/GP $ python manage.py runscript test_script -v2
Check for www.scripts.test_script
Cannot import module 'www.scripts.test_script': No module named 'nonexistrentpackage'.
Check for django.contrib.admin.scripts.test_script
Cannot import module 'django.contrib.admin.scripts.test_script': No module named 'django.contrib.admin.scripts'.
Check for django.contrib.auth.scripts.test_script
Cannot import module 'django.contrib.auth.scripts.test_script': No module named 'django.contrib.auth.scripts'.
Check for django.contrib.contenttypes.scripts.test_script
Cannot import module 'django.contrib.contenttypes.scripts.test_script': No module named 'django.contrib.contenttypes.scripts'.
Check for django.contrib.sessions.scripts.test_script
Cannot import module 'django.contrib.sessions.scripts.test_script': No module named 'django.contrib.sessions.scripts'.
Check for django.contrib.messages.scripts.test_script
Cannot import module 'django.contrib.messages.scripts.test_script': No module named 'django.contrib.messages.scripts'.
Check for django.contrib.staticfiles.scripts.test_script
Cannot import module 'django.contrib.staticfiles.scripts.test_script': No module named 'django.contrib.staticfiles.scripts'.
Check for django_extensions.scripts.test_script
Cannot import module 'django_extensions.scripts.test_script': No module named 'django_extensions.scripts'.
Check for scripts.test_script
Cannot import module 'scripts.test_script': No module named 'scripts'.
No (valid) module for script 'test_script' found
</code></pre>

<p>where we can see the crucial line:</p>

<pre><code>No module named 'nonexistrentpackage'.
</code></pre>

<p>The commonality of the errors seems to be because the extension runs the script using <code>import</code>. It would be more sensible if it first checked for the existence of the file using <a href="https://docs.python.org/3/library/os.path.html" rel="nofollow"><code>os.path.isfile</code></a> and if not found, the threw a more sensible error message.</p>

python django