[Python] Exception when trying to launch django project debug in VS code

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    Hi I have a django project (4.2.2) and when I start the project from the terminal inside VS Code using "python3 manage.py runserver" everything works perfectly. But I want to debug it so I created a launch.json file to start it as debug Here is what it contains:

    {
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
    {
    "name": "Python Debugger django",
    "type": "debugpy",
    "request": "launch",
    "program": "${workspaceFolder}/manage.py",
    "console": "integratedTerminal",
    "django": true,
    "args": [
    "runserver",
    ]
    }
    ]
    }


    When I try to start the project using debug button I get the following error: django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 or psycopg module

    I have installed psycopg library as if I look at my requiremnts.txt I have

    psycopg==3.2.4
    psycopg-binary==3.2.4


    Not sure what is the problem? In debug mode the error is display in my models.py file:

    from django.db import models

    class User(models.Model): # <-- here is the error
    """
    User entity
    """
    id = models.AutoField(primary_key=True)
    first_name = models.CharField(max_length=80)
    last_name = models.CharField(max_length=100)
    # created_at = models.DateTimeField(auto_now_add=True)


    def __str__(self):
    return f"{self.first_name} {self.last_name}"


    EDIT: added full error trace below

    Exception in thread django-main-thread:
    Traceback (most recent call last):
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 25, in <module>
    import psycopg as Database
    ModuleNotFoundError: No module named 'psycopg'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 27, in <module>
    import psycopg2 as Database
    ModuleNotFoundError: No module named 'psycopg2'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
    File "/usr/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 126, in inner_run
    autoreload.raise_last_exception()
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 394, in execute
    autoreload.check_errors(django.setup)()
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/apps/registry.py", line 116, in populate
    app_config.import_models()
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/apps/config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
    File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
    File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
    File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 883, in exec_module
    File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
    File "/home/louis/zero/backend/ztm_app/models.py", line 3, in <module>
    class User(models.Model):
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/db/models/base.py", line 143, in __new__
    new_class.add_to_class("_meta", Options(meta, app_label))
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/db/models/base.py", line 371, in add_to_class
    value.contribute_to_class(cls, name)
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/db/models/options.py", line 231, in contribute_to_class
    self.db_table, connection.ops.max_name_length()
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/db/utils.py", line 193, in create_connection
    backend = load_backend(db["ENGINE"])
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/db/utils.py", line 113, in load_backend
    return import_module("%s.base" % backend_name)
    File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
    File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
    File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 883, in exec_module
    File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
    File "/home/louis/zero/backend/venv/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 29, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 or psycopg module")
    django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 or psycopg module

    Continue reading...

Compartilhe esta Página