0

Using PyCharm Communuty: trying to run my PyCharm console with folowing code to do some tests but unfortunately facing the problem described below. Really appreciate your attention

from django.db import models


# Create your models here.
class ProductCategory(models.Model):
    name = models.CharField(max_length=128, unique=True)
    description = models.TextField(null=True, blank=True)

    def __str__(self):
        return self.name


class Product(models.Model):
    name = models.CharField(max_length=256)
    description = models.TextField()
    price = models.DecimalField(max_digits=6, decimal_places=2)
    quantity = models.PositiveIntegerField(default=0)
    image = models.ImageField(upload_to='products_images')
    category = models.ForeignKey(to=ProductCategory, on_delete=models.CASCADE)

Then trying to run PyCharm console and execute the folowing line:

from products.models import ProductCategory

Then getting error:

Traceback (most recent call last):
  File "/snap/pycharm-community/310/plugins/python-ce/helpers/pydev/pydevconsole.py", line 364, in runcode
    coro = func()
  File "<input>", line 1, in <module>
  File "/snap/pycharm-community/310/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/home/verts/Documents/Last/course/store-server/store/products/models.py", line 5, in <module>
    class ProductCategory(models.Model):
  File "/home/verts/Documents/Last/course/store-server/store/venv/lib/python3.9/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/verts/Documents/Last/course/store-server/store/venv/lib/python3.9/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "/home/verts/Documents/Last/course/store-server/store/venv/lib/python3.9/site-packages/django/apps/registry.py", line 135, in check_apps_ready
    settings.INSTALLED_APPS
  File "/home/verts/Documents/Last/course/store-server/store/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/home/verts/Documents/Last/course/store-server/store/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 63, 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.

App 'products' is registered in settings .. :(

docoder
  • 19
  • 5

2 Answers2

1

Follow these instructions: How to set environment variables in PyCharm? and add a new environmental variable

DJANGO_SETTINGS_MODULE=MyDjangoProject.settings

Change MyDjangoProject to the directory your settings.py is in.

You can read more about how it works specifically with Django here: https://docs.djangoproject.com/en/4.1/topics/settings/#designating-the-settings

Nealium
  • 2,025
  • 1
  • 7
  • 9
  • No problem yo. Obviously wasn't the best. You know.. You wouldn't have these issues if you used a shell ;) – Nealium Dec 16 '22 at 16:32
1

You should open the settings and search for django console. Add environment variable and point to your settings file. It should be a permanent fix. Hope it helps enter image description here