Django's runscript: No (valid) module for script 'filename' found

Question:

I’m trying to run a script from the Django shell using the Django-extension RunScript. I have done this before and but it refuses to recognize my new script:

(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

While running any other script works fine:

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

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:

(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

and the error is the same.

Why can’t RunScript find my file?

Asked By: CoderGuy123

||

Answers:

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.

Here’s an example script to produce the error:

import nonexistrentpackage

def run():
    print("Test")

The example has the only stated requirement for scripts, namely a run function.

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

(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

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

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

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 first (scroll up) error returned:

(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

where we can see the crucial line:

No module named 'nonexistrentpackage'.

The commonality of the errors seems to be because the extension runs the script using import. It would be more sensible if it first checked for the existence of the file using os.path.isfile and if not found, the threw a more sensible error message.

Answered By: CoderGuy123

I had the same issue. I spend an hour to find out what is the problem. However, the solution is very simple. In my case, I had the following modules imported in my script.

from openpyxl import Workbook
from openpyxl.styles import Font
from openpyxl.utils import get_column_letter

I was using virtualenv and I forgot to install openpyxl.

I have installed opnepyxl using pip.

pip install openpyxl

So please make sure all of your imported modules are installed.

Hope that will help.

Answered By: user1012513

Another Important thing to watchout for

No .py should be given, just the name of the file ( script )

 python3 manage.py runscript -v3 scriptname;
Answered By: indianwebdevil

You are most likely not running the runscript command correctly.

Scenario A: You script is inside the scripts folder
i.e.

   scripts
     test.py

run python manage.py runscript test

Scenario B: Your script is inside a sub directory
i.e

   scripts
     data
       data.py

run python manage.py runscript scripts.data.data

Scenario C:
Your scrips is in a folder that isn’t scripts. Follow instructions in scenario B above.

Answered By: Isaac Sekamatte

You can verificate if the name of your script folder is correctly write. the folder name can be "scripts" with "s" to the end. I’ve forget him and a have a problem

Answered By: Emile

I have another option for this:

I named the script filename xx_v1.6.0.py.

python manage.py runscript xx_v1.6.0
# No (valid) module for script 'xx_v1.6.0' found
# Try running with a higher verbosity level like: -v2 or -v3

After I changed filename to xx.py.

python manage.py runscript xx

Everything works fine.

Try using

python manage.py shell < scripts/{$filename$}

It should work the same and be easier.

Answered By: Ben
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.