Hmm, I think Conrad might have sent you down a blind alley with this!
Instead of trying to set up the Django settings in your script, it's best to write a Django management command. That will do all of the setup for you and means that you don't need to do any special configuration; you can just put code into it that would work in manage.py shell.
As an aside:
manage.py shell is a general Django thing, not PyCharm-specific -- if you've installed your Django app into
/home/josh87/mydjangosite then running
/home/josh87/mydjangosite/manage.py shell will give you a shell where you can do stuff just like you can locally -- of course, that's useful interactively, but we're talking about scheduling things here, so it's not what we need.
Anyway, to write a management command, you need to create a file inside your Django project. Let's say you have an app inside your
mydjangosite project called
myapp, so it's stored in the directory
/home/josh87/mydjangosite/myapp/. Inside that directory, you create a directory called
management, and inside there you create one called
commands. Finally, in there, you create a Python file called, say,
mycommand.py. In that file, you can put code like this:
from datetime import datetime
from django.core.management.base import BaseCommand
# Import your Django models here
help = 'Runs my special command'
def add_arguments(self, parser):
def handle(self, *args, **options):
if datetime.today().day != 15:
# Your Django code goes here
Once you've done that, you'll have a new thing you can run using
manage.py -- if you schedule the script
...then it will run the Django code in the
The full documentation for Django management commands is here.