Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

django

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?

like image 889
CoderGuy123 Avatar asked Sep 05 '16 01:09

CoderGuy123


3 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.

like image 148
CoderGuy123 Avatar answered Nov 11 '22 17:11

CoderGuy123


Another Important thing to watchout for

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

 python3 manage.py runscript -v3 scriptname;
like image 7
indianwebdevil Avatar answered Nov 11 '22 17:11

indianwebdevil


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.

like image 1
user1012513 Avatar answered Nov 11 '22 16:11

user1012513