In my case it was the use of the call_command
module that posed a problem.
I added set DJANGO_SETTINGS_MODULE=mysite.settings
but it didn't work.
I finally found it:
add these lines at the top of the script, and the order matters.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
import django
django.setup()
from django.core.management import call_command