Having troubles with simple updates of the database

I am using django and I am trying to have a script run in the background which updates some values in my MySQL database. However, I am having some troubles making it work even when I try to run my script manually. It seems like the main issue is importing the models that is to be updated. For instance when I format the import in the following way

from .models import bokningsData

I get the error

File "/home/skallars/mysite/webapp/", line 2, in <module> from .models import bokningsData ModuleNotFoundError: No module named '__main__'.models; '__main__' is not a package

The above formatting works however in VSCode.

When I format as

from models import bokningsData

I get the error

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJAN GO_SETTINGS_MODULE or call settings.configure() before accessing settings."

If I try to run it as a background task I get the error

ValueError: Attempted relative import in non-package

If I just copy the code in my file to and run it manually, I get the first two errors depending on the formatting. However, when run by getting triggered when I go to the webpage, it works and the database gets updated. It also works when I update the database manually via the shell.

What am I missing here?

Got it workning by adding

import sys

import os

import django


os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'


That would work well. If you're going to do lots of scripts like this, which need to use Django from outside the context of the website, then I'd recommend looking into Django management commands.