Skip to content
Advertisement

Django Standalone Script in Django 4.1

I am trying to run a standalone script in django 4.1. I have the setup as suggested in this post at the top of my file:

Django Standalone Script

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "path_to_settings.settings")
import django
django.setup()

However, on the 4th line, I get the following error:

ModuleNotFoundError: No module named 'sport_api'

This is my INSTALLED_APPS in settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # applications
    'sport_api',
    'sport_bet',

    # library
    'rest_framework',
    'corsheaders',
]

How do I get around this error?

Update:

Directory Structure

Advertisement

Answer

You have to move the script to the manage.py layer. That’s the only thing that got this to work for me.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement