0

I am using Django for the first time but have used PostgreSQL previously. I am trying to follow the official Django tutorial to set up with a database. I have followed everything but I get an error when using the command "python manage.py migrate" that psycopg2 is not found even though I have it

installed.Traceback (most recent call last):
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/backends/postgresql/base.py", line 24, 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 "/Users/alexanderverheecke/Documents/GitHub/mysite/manage.py", line 22, in <module>
    main()
  File "/Users/alexanderverheecke/Documents/GitHub/mysite/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/core/management/__init__.py", line 420, in execute
    django.setup()
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/apps/registry.py", line 116, in populate
    app_config.import_models()
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/apps/config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/models/base.py", line 141, in __new__
    new_class.add_to_class("_meta", Options(meta, app_label))
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/models/base.py", line 369, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/models/options.py", line 231, in contribute_to_class
    self.db_table, connection.ops.max_name_length()
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/utils.py", line 193, in create_connection
    backend = load_backend(db["ENGINE"])
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/utils.py", line 113, in load_backend
    return import_module("%s.base" % backend_name)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/backends/postgresql/base.py", line 28, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

I am using the settings as mentioned in the Django tutorial:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'DatabaseName',
        'USER': 'UserName',
        'PASSWORD': 'Userpassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

I have confirmed that I have the necessary packages installed and in the same location :

pip freeze:

Django==4.1.3
psycopg2==2.9.5
psycopg2-binary==2.9.5

pip show Django/psycopg2/psycopg2-binary:

Django : Location: /opt/homebrew/lib/python3.10/site-packages
Psycopg2: Location: /opt/homebrew/lib/python3.10/site-packages
psycopg2-binary: Location: /opt/homebrew/lib/python3.10/site-packages
  • Does this answer your question? [django.core.exceptions.ImproperlyConfigured: Error loading psycopg module: No module named psycopg](https://stackoverflow.com/questions/8237842/django-core-exceptions-improperlyconfigured-error-loading-psycopg-module-no-mo) – Amir reza Riahi Nov 17 '22 at 14:30
  • Unfortunately, I tried the solutions listed there too but it still is not working. – Alexander Veheecke Nov 17 '22 at 15:06
  • As identified by @rm_kuzmin, it seems like you have two different python installations that you're using at different times. The `pip freeze` output suggests that you have a Python 3.10 environment which has django and psycopg2 installed, but then the python that is used for your application is 3.9, and probably doesn't have psycopg2 installed. The output of `which python`, `which pip`, `pip debug` would help here – KevinOrr Nov 20 '22 at 15:00

1 Answers1

0

It's seems like you use system python for running your migrations. Error traceback contains following path of python binary: "/Users/alexanderverheecke/Library/Python/3.9/...", however in pip show command your python path is "/opt/homebrew/lib/python3.10/".

Actually I don't understand how it's even possible, because first one looks like Windows path and the second one like Linux...

Anyway. Try something of this and run migrate command again:

  • activate/deactivate virtual environment if you use it
  • change your OS to the correct one
  • run pip install again

Make shure command which python(for Linux), gcm python (for Windows) returns the same python path as pip show.

rm_kuzmin
  • 26
  • 2
  • Both of these paths suggest MacOS, not Windows or Linux, though you're likely right that the asker is probably mixing up two different python installations – KevinOrr Nov 20 '22 at 14:54