django runscript error: no (valid) module for script 'main' found

Viewed 218

I wrote some code to run ... but i had a error while running my code. So I tested some simple code like this... but it still doesn't work.

def run():
  print("test")

Error message looks like:

Error Message

which says no (valid) module for script 'main' found. How can I fix it?

1 Answers

Running Extra scripts in Django:

  1. Create scripts directory in your root project, next to manage.py:

    $ mkdir scripts
    
  2. Add an empty file init.py (double underscore) into your /scripts directory:

    $ touch scripts/__init__.py
    

    The init file is necessary so that the folder is picked up as a python package.

    This is probably what you're missing.

  3. Add your scripts into /scripts directory:

    $ touch scripts/main.py
    
  4. After you edited your main.py script, run it with:

    $ python manage.py runscript script-name
    

    Note: You shouldn’t include .py when specifying the script to run

Checkout Runscript Documentation

Related