I get configure error when i try to launch my projects in debug mode

Here’s my manage.py:

#!/usr/bin/env python
“”“Django’s command-line utility for administrative tasks.”“”
import os
import sys

def main():
“”“Run administrative tasks.”“”
os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘webstore.settings’)
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn’t import Django. Are you sure it’s installed and "
"available on your PYTHONPATH environment variable? Did you "
“forget to activate a virtual environment?”
) from exc
execute_from_command_line(sys.argv)

if name == ‘main’:
main()

Here’s the error:

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

please share:

  1. your project structure
  2. your full error log

To me, it seems like the line os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘webstore.settings’) is the issue and your settings file path is wrong.

Welcome @TigrBross !

How are you running this in debug mode?

  • From the command line?
  • From within an IDE?

If from the command line, what is the full command you are running?

  • Is the virtual environment active?
  • Is the current directory correct?

If you’re running this from within an IDE, which IDE?

  • What is your configuration for debug mode in that IDE?
  • Is it activating the right virtual environment?
  • Is it running it from the correct directory?
  1. What do you mean by structure?
  2. “D:\xary\Tigrani gortser\self_lessons\venvs\webstore_env\Scripts\python.exe” -X pycache_prefix=C:\Users\user\AppData\Local\JetBrains\PyCharmCE2023.3\cpython-cache “C:/Program Files/JetBrains/PyCharm Community Edition 2023.3.4/plugins/python-ce/helpers/pydev/pydevd.py” --multiprocess --qt-support=auto --client 127.0.0.1 --port 57214 --file “D:\xary\Tigrani gortser\self_lessons\djangoprojects\webstore\main\utils.py”
    Connected to pydev debugger (build 233.14475.56)
    Traceback (most recent call last):
    File “”, line 1176, in _find_and_load
    File “”, line 1147, in _find_and_load_unlocked
    File “”, line 690, in _load_unlocked
    File “”, line 940, in exec_module
    File “”, line 241, in call_with_frames_removed
    File “D:\xary\Tigrani gortser\self_lessons\djangoprojects\webstore\goods\models.py”, line 11, in
    class Categories(Model):
    File “D:\xary\Tigrani gortser\self_lessons\venvs\webstore_env\Lib\site-packages\django\db\models\base.py”, line 129, in new
    app_config = apps.get_containing_app_config(module)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\xary\Tigrani gortser\self_lessons\venvs\webstore_env\Lib\site-packages\django\apps\registry.py”, line 260, in get_containing_app_config
    self.check_apps_ready()
    File “D:\xary\Tigrani gortser\self_lessons\venvs\webstore_env\Lib\site-packages\django\apps\registry.py”, line 137, in check_apps_ready
    settings.INSTALLED_APPS
    File "D:\xary\Tigrani gortser\self_lessons\venvs\webstore_env\Lib\site-packages\django\conf_init
    .py", line 89, in getattr
    self.setup(name)
    File "D:\xary\Tigrani gortser\self_lessons\venvs\webstore_env\Lib\site-packages\django\conf_init
    .py", line 69, in _setup
    raise ImproperlyConfigured(
    django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
    python-BaseException
    Traceback (most recent call last):
    File “C:\Program Files\JetBrains\PyCharm Community Edition 2023.3.4\plugins\python-ce\helpers\pydev_pydevd_bundle\pydevd_xml.py”, line 177, in _get_type
    if isinstance(o, t[0]):
    ^^^^^^^^^^^^^^^^^^^
    File “D:\xary\Tigrani gortser\self_lessons\venvs\webstore_env\Lib\site-packages\django\utils\functional.py”, line 280, in getattribute
    value = super().getattribute(name)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “D:\xary\Tigrani gortser\self_lessons\venvs\webstore_env\Lib\site-packages\django\utils\functional.py”, line 251, in inner
    self.setup()
    File "D:\xary\Tigrani gortser\self_lessons\venvs\webstore_env\Lib\site-packages\django\conf_init
    .py", line 69, in _setup
    raise ImproperlyConfigured(
    django.core.exceptions.ImproperlyConfigured: Requested settings, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Process finished with exit code 1

Im running debug using pycharm.

I am using active virtual enviroment
All directories are right i guess

Is this a file you’re trying to run individually?

If you’re not running this within the context of something like wsgi.py, the runserver command, or a custom management command, it’s up to you to initialize the Django environment in your program before running your code. You can’t just run individual Django functions independently.

Ohhh, then what file am i supposed to run?

What are you looking to do? (What is the code in this utils.py that you’re looking to test.)

from urllib.parse import unquote

from django.contrib.postgres.search import SearchVector, SearchQuery, SearchRank, SearchHeadline
from django.db.models import Q
from django.db.models.functions import Lower

from goods.models import Categories, Products


class DataMixin:
    def get_user_context(self, **kwargs):
        context = kwargs
        context['categories'] = Categories.objects.all()
        return context


def q_search(query):
    query = unquote(query)
    if query.isdigit() and len(query) == 5:
        return Products.objects.filter(ident=query)
    elif not query.isdigit():
        vector = SearchVector('name', 'description')
        query = SearchQuery(query)
        result = Products.objects.annotate(rank=SearchRank(vector, query)).filter(rank__gt=0).order_by("-rank")

        result = result.annotate(
            headline=SearchHeadline('name', query, start_sel='<span style="background-color: yellow;">', stop_sel='</span>'))
        result = result.annotate(
            bodyline=SearchHeadline('description', query, start_sel='<span style="background-color: yellow;">', stop_sel='</span>'))

        return result

        # return Products.objects.annotate(search=SearchVector('name', 'description')).filter(search=query)

        # keywords = [word for word in query.split() if len(word) > 1]
        #
        # q_objects = Q()
        #
        # for token in keywords:
        #     q_objects |= Q(description__icontains=token)
        #     q_objects |= Q(name__icontains=token)
        #
        # return Products.objects.filter(q_objects)

I wanted to test search method and see the variables while site is running

If you’re looking to examine this in the context of a running web site, then it’s the entire website process that needs to be run in debug mode. (In other words, if you’re using runserver to run your site for development, then it’s the runserver command that you want to debug.)

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of fixing your post.)

1 Like

Thank you. And how do i use runserver in debug mode?

The runserver command is a django-admin subcommand, typically run by using the manage.py script. So what you’ll actually run to debug is manage.py. (I don’t use PyCharm, so I don’t know how it’s done there. They probably have information on their site.)

Thank you very much!