相关文章推荐

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hi all, First of all, I've read all the posts I could find about this problem to no avail :(

I've got an up&running default webapp with python3 and django already working with default db.sqlite2. I now need to change all the db backend to psql.

So, from these sources:

https://pypi.org/project/psycopg2/
https://www.psycopg.org/docs/install.html

I've installed psycopg2-binary, OK. Then, from this source:

https://www.enterprisedb.com/postgres-tutorials/how-use-postgresql-django

I've modified settings.py, DATABASES section, to use psql instead of sqlite. I've tried:

'ENGINE': 'django.db.backends.postgresql_psycopg2',

and it failed. Then, from some youtube videos, I tried:

'ENGINE': 'django.db.backends.postgresql',

which also fails... The error is this one, when I try to make migrations:

(env) PS D:\Users\llagos.NB-KEYONE15\Visual Studio projects\ecommerce-psql> python manage.py makemigrations
Traceback (most recent call last):
File "D:\Users\llagos.NB-KEYONE15\Visual Studio projects\ecommerce-psql\env\lib\site-packages\django\db\backends\postgresql\base.py", line 25, in
import psycopg2 as Database
ModuleNotFoundError: No module named 'psycopg2'
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
So, what's wrong with this settings.py file? is this correct? I've tried to google for information but again, no luck. Documentation for psycopg2 doesn't say what to change on settings.py. It just say "install and code", but when using django, there are previous steps, like this settings.py file.

My env:

(env) PS D:\Users\llagos.NB-KEYONE15\Visual Studio projects\ecommerce-psql> pip freeze
asgiref==3.3.4
astroid==2.5.6
colorama==0.4.4
Django==3.2.2
isort==5.8.0
lazy-object-proxy==1.6.0
mccabe==0.6.1
psycopg2-binary==2.8.6
pylint==2.8.2
pylint-django==2.4.4
pylint-plugin-utils==0.6
pytz==2021.1
sqlparse==0.4.1
toml==0.10.2
wrapt==1.12.1
(env) PS D:\Users\llagos.NB-KEYONE15\Visual Studio projects\ecommerce-psql>

So far, those are all my changes... Again, this is happily working with default dblite. I just installed the module, and modified the settings file.

Thanks a lot for any hint!

Regards,

Same here. What Python version do you use? I am running it in a conda environment inside a docker container. I then used
pip install psycopg2-binary

And that worked.

Anybody can help me to resolve the error.

PS C:\Users\Gautam\OneDrive\Desktop\Project_ERP\ERP> python manage.py syncdb
Traceback (most recent call last):
File "C:\Python312\Lib\site-packages\django\db\backends\postgresql\base.py", line 25, in
import psycopg as Database
File "C:\Python312\Lib\site-packages\psycopg_init_.py", line 9, in
from . import pq # noqa: F401 import early to stabilize side effects
^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\psycopg\pq_init_.py", line 114, in
import_from_libpq()
File "C:\Python312\Lib\site-packages\psycopg\pq_init_.py", line 106, in import_from_libpq
raise ImportError(
ImportError: no pq wrapper available.
Attempts made:

  • couldn't import psycopg 'c' implementation: No module named 'psycopg_c'
  • couldn't import psycopg 'binary' implementation: No module named 'psycopg_binary'
  • couldn't import psycopg 'python' implementation: libpq library not found
  • During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "C:\Python312\Lib\site-packages\django\db\backends\postgresql\base.py", line 27, in
    import psycopg2 as Database
    File "C:\Python312\Lib\site-packages\psycopg2_init_.py", line 51, in
    from psycopg2._psycopg import ( # noqa
    ImportError: DLL load failed while importing _psycopg: The specified module could not be found.

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "C:\Users\Gautam\OneDrive\Desktop\Project_ERP\ERP\manage.py", line 22, in
    main()
    File "C:\Users\Gautam\OneDrive\Desktop\Project_ERP\ERP\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
    File "C:\Python312\Lib\site-packages\django\core\management_init_.py", line 442, in execute_from_command_line
    utility.execute()
    File "C:\Python312\Lib\site-packages\django\core\management_init_.py", line 416, in execute
    django.setup()
    File "C:\Python312\Lib\site-packages\django_init_.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
    File "C:\Python312\Lib\site-packages\django\apps\registry.py", line 116, in populate
    app_config.import_models()
    File "C:\Python312\Lib\site-packages\django\apps\config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Python312\Lib\importlib_init_.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "", line 1381, in _gcd_import
    File "", line 1354, in _find_and_load
    File "", line 1325, in _find_and_load_unlocked
    File "", line 929, in _load_unlocked
    File "", line 994, in exec_module
    File "", line 488, in _call_with_frames_removed
    File "C:\Python312\Lib\site-packages\django\contrib\auth\models.py", line 3, in
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
    File "C:\Python312\Lib\site-packages\django\contrib\auth\base_user.py", line 57, in
    class AbstractBaseUser(models.Model):
    File "C:\Python312\Lib\site-packages\django\db\models\base.py", line 143, in new
    new_class.add_to_class("_meta", Options(meta, app_label))
    File "C:\Python312\Lib\site-packages\django\db\models\base.py", line 371, in add_to_class
    value.contribute_to_class(cls, name)
    File "C:\Python312\Lib\site-packages\django\db\models\options.py", line 243, in contribute_to_class
    self.db_table, connection.ops.max_name_length()
    ^^^^^^^^^^^^^^
    File "C:\Python312\Lib\site-packages\django\utils\connection.py", line 15, in getattr
    return getattr(self._connections[self.alias], item)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
    File "C:\Python312\Lib\site-packages\django\utils\connection.py", line 62, in getitem
    conn = self.create_connection(alias)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Python312\Lib\site-packages\django\db\utils.py", line 193, in create_connection
    backend = load_backend(db["ENGINE"])
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Python312\Lib\site-packages\django\db\utils.py", line 113, in load_backend
    return import_module("%s.base" % backend_name)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Python312\Lib\importlib_init
    .py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Python312\Lib\site-packages\django\db\backends\postgresql\base.py", line 29, in
    raise ImproperlyConfigured("Error loading psycopg2 or psycopg module")
    django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 or psycopg module

    @Gautamneeraj88 See #1628: either you use Python 3.11 or you use psycopg 3. Psycopg 2 for Python 3.12 will be released in a few days or week, when the Appveyor CI builders are available.

    For psycopg 3 you can pip install psycopg[binary]. See the docs.

    Hello @dvarrazzo , I am also facing the same error .
    Python version :- [ Python 3.12.2 ]
    Postgres Version :- 16

    If possible , Could you share some resource on creating a Json table to store the Json Data from the file Directory.

    Since psycopg2 is causing issues, it might be better to switch to the newer psycopg package, which is more compatible with modern Python versions like 3.12. It helped me:

    poetry add psycopg

     
    推荐文章