Error loading psycopg2 module dll load failed while importing psycopg

TL; DR: update pip. See this comment I get this error when trying to import psycopg2 in Python 3.8 on Windows 10. I already tried reinstalling (both psycopg2 and psycopg2-binary) and even reinstall...

I created a project yesterday evening with 3.8.2 and ran into this problem. Error can be found below.
This morning I did a new venv with 3.7.6, exact same setup besides that, and there was no issue.

One remark… When installing the package on python38 (the -binary), it seems to compile it… But not on python37…
Installing psycopg2-binary on python37: https://gist.github.com/tobiasgardner/57ac84e45c8c5502ea5682852e300a31
Installing psycopg2-binary on python38: https://gist.github.com/tobiasgardner/7f93fcd22e413b525833adfe75c54d92

C:UserstobbeAppDataLocalJetBrainsToolboxappsPyCharm-Pch-0193.6494.30binrunnerw64.exe C:UserstobbeDocumentsgit-reposecombooster.venvScriptspython.exe C:/Users/tobbe/Documents/git-repos/ecombooster/manage.py runserver 8000 Watching for file changes with StatReloader Exception in thread django-main-thread: Traceback (most recent call last): File "C:Program FilesPython38libthreading.py", line 932, in _bootstrap_inner self.run() File "C:Program FilesPython38libthreading.py", line 870, in run self._target(*self._args, **self._kwargs) File "C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangoutilsautoreload.py", line 53, in wrapper fn(*args, **kwargs) File "C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangocoremanagementcommandsrunserver.py", line 109, in inner_run autoreload.raise_last_exception() File "C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangoutilsautoreload.py", line 76, in raise_last_exception raise _exception[1] File "C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangocoremanagement__init__.py", line 357, in execute autoreload.check_errors(django.setup)() File "C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangoutilsautoreload.py", line 53, in wrapper fn(*args, **kwargs) File "C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjango__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangoappsregistry.py", line 91, in populate app_config = AppConfig.create(entry) File "C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangoappsconfig.py", line 116, in create mod = import_module(mod_path) File "C:Program FilesPython38libimportlib__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 671, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 783, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangocontribpostgresapps.py", line 4, in <module> from psycopg2.extras import DateRange File "C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagespsycopg2__init__.py", line 50, in <module> from psycopg2._psycopg import ( # noqa ImportError: DLL load failed while importing _psycopg: The specified module could not be found.

I installed psycopg2 using conda on Windows 10.

https://anaconda.org/anaconda/psycopg2

I did it in a clean new conda environment (named wr).

I then tried to run this sample app but I am getting this error (see below).
I have no idea what I might be doing wrong because it was all straightforward and I did it in a clean way.

Any ideas how to solve this?

import psycopg2
try:
    connection = psycopg2.connect(user = "***",
                                  password = "***",
                                  host = "***",
                                  port = "5432",
                                  database = "***")


    cursor = connection.cursor()
    # Print PostgreSQL Connection properties
    print ( connection.get_dsn_parameters(),"n")

    # Print PostgreSQL version
    cursor.execute("SELECT version();")
    record = cursor.fetchone()
    print("You are connected to - ", record,"n")

except (Exception, psycopg2.Error) as error :
    print ("Error while connecting to PostgreSQL", error)
finally:
    #closing database connection.
        if(connection):
            cursor.close()
            connection.close()
            print("PostgreSQL connection is closed")

Error in VS code:

PS C:WorkWRRgittoolsJTunnelTestApp>  cd 'c:WorkWRRgittoolsJTunnelTestApp'; & 'C:ProgramsAnaconda3envswrpython.exe' 'c:Userspetrop01.vscodeextensionsms-python.python-2020.9.114305pythonFileslibpythondebugpylauncher' '56143' '--' 'c:WorkWRRgittoolsJTunnelTestAppmain.py'
Traceback (most recent call last):
  File "c:WorkWRRgittoolsJTunnelTestAppmain.py", line 1, in <module>
    import psycopg2
  File "C:ProgramsAnaconda3envswrlibsite-packagespsycopg2__init__.py", line 51, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError: DLL load failed while importing _psycopg: The operating system cannot run %1.
PS C:WorkWRRgittoolsJTunnelTestApp>

EDIT: Seems they had a bug open for this 2 years ago and they just closed it, ignoring it completely.

https://github.com/psycopg/psycopg2/issues/734

Содержание

  1. Python 3.8: ImportError: DLL load failed while importing _psycopg: The specified module could not be found. #1006
  2. Comments
  3. Python 3.8: ImportError: DLL load failed while importing _psycopg: The specified module could not be found. #1014
  4. Comments
  5. Unable to install python 3.8 windows 10 #990
  6. Comments

Python 3.8: ImportError: DLL load failed while importing _psycopg: The specified module could not be found. #1006

TL; DR: update pip. See this comment

I get this error when trying to import psycopg2 in Python 3.8 on Windows 10. I already tried reinstalling (both psycopg2 and psycopg2-binary) and even reinstalling Postgres. Seems to work fine on Python 3.7.

The text was updated successfully, but these errors were encountered:

I think this is actually a different issue, this is a problem with importing the library, not with installation.

You just have installed Postgres in your os and because library installs successfully.

Even Im facing same issue. Any luck with this ?

Nope, looks like this won’t get fixed until Appveyor adds Python 3.8 support. I had to just downgrade back to Python 3.7 for now. :/

So the fix is waiting for new release to support 3.8 and walkaround is switching to 3.7 ?

FYI, if someone wants to fix the appveyor script and install Python 3.8 there for the build, be my guest.

So correct me if i’m wrong, but what i got from comments in other related issue threads here, the appveyor change/update is required to build and provide the binary package of psycopg2 for Python 3.8.

The problem is that the issue reported here, contrary to #1004, doesn’t seem to be related to the lack of the binary package.

Because of the lack of a binary version, the package seems to be built by pip from sources locally, and i can also build it myself by downloading it manually.
In both cases it doesn’t work when imported (at runtime) in Python 3.8 while it does in Python 3.7.
I clearly do pass the build prerequisites and runtime requirements.
The same error appears in Python 3.7 when i remove psql bin folder from PATH.
Is there a way to get the exact dll file that was not found from the ImportError? maybe that would shine some light here.
Should the appveyor issue also affect building the package from sources locally?

To be honest, psycopg2’s setup.py does a VERY poor job of building from source on end user’s Windows. If someone is interested in improving it.

The windows environment has several challenges when building from source vs LINUX/UNIX, some of which:

  • Need to have the correct Visual C compiler installed based upon Python version (how many of us have Visual C++ 14 installed?)
  • Detect the location of the PostgreSQL build dependencies (includes/libs)
  • Detect if we also need any OpenSSL dependencies based upon the PostgreSQL build and where those lib/include files are located
  • If linking against DLL version of libpq, make sure all the appropriate DLLs (and correct versions) are accessible via the path

Also, the PostgreSQL binaries were probably built with a newer version of the Visual C compiler, causing the potential for multiple LIBC libraries being included into the process space when pscopyg2 is imported (This shouldn’t be the case, but past experience is why we build ALL dependencies in Appveyor).

Appveyor has added Python 3.8 support. Could this please be a priority now?

I’m having this same problem.

Traceback (most recent call last):
File «main.py», line 9, in
import kepo
ImportError: DLL load failed while importing kepo: The specified module could not be found.

how do i fix this problem

Hi folks, I had the same problem and using python 3.7.2, I don’t know if it works on python 3.8 or not, but I just downgraded psycopg2-binary to 2.7.* and everything is good.

Febrary 2020 — still the same problem

@vsmelov what pip version are you using? What is the command output, possibly with a -v ?

So, Smelov Vladimir @vsmelov: your observation is perfectly correct: it is February 2020. You know your calendar. Now, in this rainy month, what brings you to dump a passive-aggressive comment on a closed bug? Because this bug has been long closed and last psycopg release is compatible indeed with Python 3.8. Binary packages are tested after build and do work for me.

If you think you have a problem please report details of what you are trying to do and about your environment. Otherwise you are just someone who thinks he is on twitter and want to create toxic atmosphere, which is a disrespectful behaviour for people who have made available for your their work for free, and for which I have no sympathy in my project.

Waiting for your answer.

I created a project yesterday evening with 3.8.2 and ran into this problem. Error can be found below.
This morning I did a new venv with 3.7.6, exact same setup besides that, and there was no issue.

One remark. When installing the package on python38 (the -binary), it seems to compile it. But not on python37.
Installing psycopg2-binary on python37: https://gist.github.com/tobiasgardner/57ac84e45c8c5502ea5682852e300a31
Installing psycopg2-binary on python38: https://gist.github.com/tobiasgardner/7f93fcd22e413b525833adfe75c54d92

C:UserstobbeAppDataLocalJetBrainsToolboxappsPyCharm-Pch-0193.6494.30binrunnerw64.exe C:UserstobbeDocumentsgit-reposecombooster.venvScriptspython.exe C:/Users/tobbe/Documents/git-repos/ecombooster/manage.py runserver 8000 Watching for file changes with StatReloader Exception in thread django-main-thread: Traceback (most recent call last): File «C:Program FilesPython38libthreading.py», line 932, in _bootstrap_inner self.run() File «C:Program FilesPython38libthreading.py», line 870, in run self._target(*self._args, **self._kwargs) File «C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangoutilsautoreload.py», line 53, in wrapper fn(*args, **kwargs) File «C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangocoremanagementcommandsrunserver.py», line 109, in inner_run autoreload.raise_last_exception() File «C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangoutilsautoreload.py», line 76, in raise_last_exception raise _exception[1] File «C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangocoremanagement__init__.py», line 357, in execute autoreload.check_errors(django.setup)() File «C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangoutilsautoreload.py», line 53, in wrapper fn(*args, **kwargs) File «C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjango__init__.py», line 24, in setup apps.populate(settings.INSTALLED_APPS) File «C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangoappsregistry.py», line 91, in populate app_config = AppConfig.create(entry) File «C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangoappsconfig.py», line 116, in create mod = import_module(mod_path) File «C:Program FilesPython38libimportlib__init__.py», line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File » «, line 1014, in _gcd_import File » «, line 991, in _find_and_load File » «, line 975, in _find_and_load_unlocked File » «, line 671, in _load_unlocked File » «, line 783, in exec_module File » «, line 219, in _call_with_frames_removed File «C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagesdjangocontribpostgresapps.py», line 4, in from psycopg2.extras import DateRange File «C:UserstobbeDocumentsgit-reposecombooster.venvlibsite-packagespsycopg2__init__.py», line 50, in from psycopg2._psycopg import ( # noqa ImportError: DLL load failed while importing _psycopg: The specified module could not be found.

@tobiasgardner thank you for the report.

The psycopg packages are available here, this is where pip would fetch them from.

My questions are:

  • What is your pip version?
  • Pip should download a wheel file. What file does pip download psycopg2 download? The binary package (which I assume psycopg2-2.8.4-cp38-cp38-win_amd64.whl but let me know which) or the .tar.gz ?
  • If it downloads the .tar.gz I wonder why, so what is the output of pip download -v psycopg2 ?
  • If it downloads the .whl , why it doesn’t install it? So what is the output of pip install -v psycopg2-2.8.4-cp38-cp38-win_amd64.whl ?

On windows the binary package and the non-binary package are the same, but please try the above with psycopg2-binary instead of psycopg2 too, thank you.

@dvarrazzo Hmm, there is something strange here. Noticed now that when I check pip version from PyCharm environment (settings), it says pip version 20.0.2 for Python 3.7 BUT when I run pip list in the terminal, it says 19.0.3. For the python 3.8 environment, it is the same. But when I try to upgrade from command prompt, it says that I already have 20.0.2.

Staying in the same venv from command prompt.

I.e. your assumption that the .whl file is downloaded was wrong, I get the tar.gz files.

Since I did not get the .whl file, it is not possible to run this command: pip install -v psycopg2-2.8.4-cp38-cp38-win_amd64.whl , it results in an error message:

Источник

Python 3.8: ImportError: DLL load failed while importing _psycopg: The specified module could not be found. #1014

Just installed Python 3.8 64-bit and was able to successfully install psycopg2 with pip install psycopg2 . I received this ImportError when trying to run my project.

The text was updated successfully, but these errors were encountered:

You already said it #1006. Please follow #990.

Unlike in #990, I am able to install psycopg2 with Python 3.8 64 on Windows 10

The error indicates it is unable to load the psycopg DLL.

Based upon what I believe your file structure is, the file should exist here:
C:. venvlibsite-packagespsycopg2_psycopg.cp38-win_amd64.pyd

Also, you should be able to look in Windows Event viewer and find an error message that maybe describes the reason a little better why it failed (ie unable to find, corrupt file, etc).

The other thing, with every command prompt (or however you run your project), you need to ‘activate’ the venv, otherwise the paths will not be setup correctly, and it will not be able to find the DLL.


@BrendanMartin, it should be pycharm’s bug, however, you can run through by up pic.
I use python3.8.1-64bit ,win10, pycharm 2019.3.1 version.

Источник

Unable to install python 3.8 windows 10 #990

Windows 10 Python 3.8

Windows 10 python 3.7 — Good

The text was updated successfully, but these errors were encountered:

Appveyor doesn’t support Python 3.8 yet.

Support for Python 3.8 on Windows will be added when appveyor will add it.

When does Appveyor usually add a new version of python?

So . what do you need to fix this?

@FrankyBoy That either appveyor makes 3.8 available on their machine, or that someone contributes a patch to the appveyor.py script to install it ourselves.

Hi! Sorry for potentially stupid questions, new to python and appveyor (mainly did c# on gitlab-ci lately). Regarding that script you mentioned, am I correct that this would be some solution akin to this here: installing pypy on appveyor? Also, if I adapt the script, how can I actually test it before making a PR?

Python 3.8 is now available on AppVeyor (appveyor/ci#3142). (Where, incidentally, I have the same issue as #1006)

pip uninstall psycopg2-binary
pip install psycopg2-binary —no-cache-dir

It’s not clear whether this is closed because it’s believed the problem is fixed, but. it’s not. Trying to install either psycopg2 or psycopg2-binary on Windows via Pip will error out with a message saying «Error: pg_config executable not found.»

@Kylotan, the problem is fixed. My guess is you are inadvertently reinstalling the cached version of the previous package. @hingston provided the correct steps by adding the parameter:
—no-cache-dir
onto the pip command line.

You can verify pip is using a cache file if it displaying the following after running the pip install command:

For me also doesn’t work. For a fresh Python 3.8 installation I have:

As far as I can see psycopg2-2.8.4-cp38-cp38-win_amd64.whl is there, at https://pypi.org/project/psycopg2/#files
Please someone debug why pip decides not to pick it up, thank you.

It works for me with both 32bit and 64bit binaries from Python.org, so there is something with your setup that is forcing it to download the source file package instead of the binary package.

One could try the following pip switch to force not use source packages, tho it doesn’t explain why it is attempting to download the sources, and I half way expect this not to work, tho hopeful the error message may explain more of what is going on:
—only-binary :all:

If the above doesn’t work, issue the following command and copy the output:
pip debug psycopg2-binary —verbose

Источник

0 / 0 / 1

Регистрация: 25.01.2018

Сообщений: 73

1

29.10.2020, 13:10. Показов 5354. Ответов 6


pip install psycopg2

pg_config is required to build psycopg2 from source. Please add the directory
containing pg_config to the $PATH or specify the full executable path with the
option:

утановил postres 13.1

pip install psycopg2

Код

Complete output (22 lines):
    running install
    running build
    running build_py
    creating build
    creating buildlib.win-amd64-3.9
    creating buildlib.win-amd64-3.9psycopg2
    copying libcompat.py -> buildlib.win-amd64-3.9psycopg2
    copying liberrorcodes.py -> buildlib.win-amd64-3.9psycopg2
    copying liberrors.py -> buildlib.win-amd64-3.9psycopg2
    copying libextensions.py -> buildlib.win-amd64-3.9psycopg2
    copying libextras.py -> buildlib.win-amd64-3.9psycopg2
    copying libpool.py -> buildlib.win-amd64-3.9psycopg2
    copying libsql.py -> buildlib.win-amd64-3.9psycopg2
    copying libtz.py -> buildlib.win-amd64-3.9psycopg2
    copying lib_ipaddress.py -> buildlib.win-amd64-3.9psycopg2
    copying lib_json.py -> buildlib.win-amd64-3.9psycopg2
    copying lib_lru_cache.py -> buildlib.win-amd64-3.9psycopg2
    copying lib_range.py -> buildlib.win-amd64-3.9psycopg2
    copying lib__init__.py -> buildlib.win-amd64-3.9psycopg2
    running build_ext
    building 'psycopg2._psycopg' extension
    error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools":

pip install —upgrade setuptools не помогает,
качать 5Гб visual studio ради одного модуля?

__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь



0



Автоматизируй это!

Эксперт Python

6481 / 4174 / 1140

Регистрация: 30.03.2015

Сообщений: 12,325

Записей в блоге: 29

29.10.2020, 13:14

2

Цитата
Сообщение от RIOCADM
Посмотреть сообщение

pip install —upgrade setuptools не помогает

железная логика) как обновление модуля установки может повлиять на

Цитата
Сообщение от RIOCADM
Посмотреть сообщение

Microsoft Visual C++ 14.0 or greater is required.

???

Цитата
Сообщение от RIOCADM
Посмотреть сообщение

качать 5Гб visual studio ради одного модуля?

угадай ответ с 1 попытки

там кстати не факт что 5 гигов, я правда не помню, но вроде можно именно только тулзы выбрать без всякой сопутствующей хрени



0



0 / 0 / 1

Регистрация: 25.01.2018

Сообщений: 73

29.10.2020, 14:23

 [ТС]

3

Установлены пакеты Visual C++ 2015-2019 Redistributable-14.25.28508

Добавлено через 59 минут
Установил VISUAL STUDIO
установил psycopg2
но модуль не экспортируется:

from psycopg2._psycopg import ( # noqa
ImportError: DLL load failed while importing _psycopg: Не найден указанный модуль.

Добавлено через 6 минут
на прошлой версии Windwos 10, все работало без проблем



0



Автоматизируй это!

Эксперт Python

6481 / 4174 / 1140

Регистрация: 30.03.2015

Сообщений: 12,325

Записей в блоге: 29

29.10.2020, 14:24

4

RIOCADM, питон надеюсь не самый свежий (3.9) ?



0



0 / 0 / 1

Регистрация: 25.01.2018

Сообщений: 73

29.10.2020, 14:26

 [ТС]

5

да самый последний 3.9, нужна более старая версия?



0



Автоматизируй это!

Эксперт Python

6481 / 4174 / 1140

Регистрация: 30.03.2015

Сообщений: 12,325

Записей в блоге: 29

29.10.2020, 14:27

6

Лучший ответ Сообщение было отмечено RIOCADM как решение

Решение

RIOCADM, ну я чет у них там вижу поддержку 3.8 только не выше, может проглядел. Я знаю что много в послденее время проблем в разных ветках именно с новым питоном, потому рискнул бы помладше поставить, вообще старый добрый 3.7



0



0 / 0 / 1

Регистрация: 25.01.2018

Сообщений: 73

29.10.2020, 14:41

 [ТС]

7

спасибо
python-3.8.6-amd64
работает.
Раньше просто на ubuntu работал с windows пока не разобрался.



0



IT_Exp

Эксперт

87844 / 49110 / 22898

Регистрация: 17.06.2006

Сообщений: 92,604

29.10.2020, 14:41

Помогаю со студенческими работами здесь

Python + psycopg2
Добрый день, ребята подскажите по модулю psycopg2.

Python + postgresql.

При выполнение SELECT…

Python 3.6 и psycopg2
Здравствуйте!

Возникает проблема с библиотекой &quot;psycopg2&quot;.

Сервер установлен и настроен, к…

Не устанавливается Windows 7 на ноутбук, где была Windows 8
Добрый вечер , купил ноут sony waio sve151101j, на нём стоbn Windows 8 , она мне показалась жутко…

На ноутбук не устанавливается Windows 7 после удаленной Windows 8
Вообщем родители купили ноут там стоял windows 8,попросили поменять,поставил windows 7.А там нету…

psycopg2: ошибка авторизации
Доброго времени суток!

Есть скрипт, работающий с БД Postgres, используя модуль psycopg2. Если…

Psycopg2 как добавить кириллицу
Имеется таблица table c русскими буквами в строках и такой код:

import psycopg2
……

Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:

7

Configuring a Django PostgreSQL project is a straight-forward process that involves updating the settings.py file and adding one additional library (psycopg2). Depending on which IDE, OS, and Python version used there are several possible errors that can arise. We’ll address those here as well.

Django comes configured to work with a local SQLite database by default, which is great for early-stage app development. Unfortunately, it’s not great for production use or even some more stringent development tasks like concurrent I/O. Below are the steps needed to configure a new Django project for use with a PostgreSQL database.

Django uses some third-party libraries by default but does not install the required libraries to support postgres. Here’s a look at the packages installed after a clean install of Django on a new virtual environment:

asgiref==3.3.1
Django==3.1.4
pytz==2020.4
sqlparse==0.4.1

Django is designed to use a project-folder sqlite database by default that is configured in the project/project/settings.py and to use the django.db.backends.sqlite3 adapter as such:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

This tells Django to use a database named BASE_DIR/db.sqlite3 as the project’s database. To use a postgres database, the following steps must happen:

  1. Specify the correct Django adapter + credentials for Postgres
  2. Install necessary 3rd party library for Postgres
  3. Specify credentials to new/existing postgres database

These steps are simple enough but can benefit from some elaboration.

Step 1: Specify Postgres Adapter + Credentials

To make Django work with Postgres, an adapter other than the default sqlite adapter must be specified. I like working with the psycopg2 Postgres adaptor and have shown how to specify that adapter below:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': "db_name",
        'USER': "db_username",
        'PASSWORD': "db_password",
        'HOST': "db_host",
        'PORT': "db_port",  # 5432 by default
    }
}

In older versions of Django, the adapter was named django.db.backends.postgresql_psycopg2. This was changed from release 1.9 forwards and summarized in the official release notes as such:

The PostgreSQL backend django.db.backends.postgresql_psycopg2 is also available as django.db.backends.postgresql. The old name will continue to be available for backward compatibility.

Obviously, you need to have access to a Postgres database to make this work well. You should replace the values in the above code with the database to which your project will connect.  Common values for HOST and PORT are localhost and 5432 respectively, which 5432 being the default Postgres port, as described in the official PostgreSQL documentation.

Step 2: Install Necessary Postgres Adaptor

While Django ships with an “adapter” for Postgres, it still needs a 3rd party library for support. If you simply add the code above and run python manage.py makemigrations you’ll get a stacktrace with the following exception:

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

Django’s django.db.backends.postgresql adaptor bridges Django’s own ORM models to Postgres, but doesn’t actually provide the adaptors needed for actual Postgres usage. Rather than re-inventing the wheel, the Django Project has elected to use the psycopg2 library—arguably the most popular python adapter for Postgres.

To make the Django adaptor work, the psycopg2 library needs to be installed with this command: pip install psycopg2. Running the pip freeze command should now produce the following output:

asgiref==3.3.1
Django==3.1.4
psycopg2==2.8.6
pytz==2020.4
sqlparse==0.4.1

Common Issues with psycopg2

Depending on what platform, Python version, or IDE used; psycopg2 has some known-issues when it comes to installation. The most common is a DLL error that will prevent Django from creating the required migrations:

ImproperlyConfigured Exception

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: DLL load failed while importing _psycopg: The specified module could not be found.

This error indicates that a core DLL file is missing from the psycopg2 installation. It’s important to recognize that psychopg2 is a third-party library and, as such, may not always be compatible with the latest Python version. In some cases, reverting to a previous version of Python may be necessary.

Option 1: Use pyscopg2-binary

Note: This package comes with its own version of some core libraries that may, under certain conditions, cause conflicts with other libraries. See the developer’s notes for more info.

Option 2: Manual Copy from System Install

Some IDEs, at least PyCharm in my experience, may have trouble installing psycopg2. In such cases, a possible workaround is to install psycopg2 to the main system install of python, then copy those files manually into your project’s virtual environment’s site-packages directory. On a Windows machine, the steps are as follows:

  1. Using the Windows terminal: pip install psycopg2
  2. Copy Program Files/PythonXX/site-packages/psycopg2 to your virtual environments’ site-packages directory.

If installed without elevated privileges, the psycopg2 library may install to the local User’s Python installation. If this is the case (you won’t find the psycopg2 package in the directory above) look in the following directory:

.
└── C:/
    └── Users/
        └── pc/
            └── AppData/
                └── Roaming/
                    └── Python/
                        └── Python38/
                            └── site-packages/
                                └── psycopg2

Migrate Changes

Assuming psycopg2 was configured correctly, your project should be adequately configured for basic Postgres usage now. Assuming your project doesn’t require any changes to models or other areas to work with Postgres, running the makemigrations and migrate commands should give you the confirmation of success you need. Below is what the output should look like—for a blank Django project having just configured Postgres:

(venv) C:pathtoyour-project>python manage.py makemigrations
No changes detected

(venv) C:pathtoyour-project>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

These are all the core Django models being migrated to the Postgres database.

Discussion

Django is an incredible framework for building multi-purpose web applications ranging from hobbyist projects to enterprise-caliber endeavors. As we’ve seen here, Django can be configured to work with PostgreSQL relatively easily. There may be some quirks depending on your OS or IDE but I’ve still to see anything that would prevent the setup outlined here from being troubleshot.

In this article, we will describe some of the possible causes that can cause Django to fail when loading psycopg, and then I will suggest possible solutions that you can use to try and resolve this issue.

Recommended

  • 1. Download ASR Pro
  • 2. Follow the on-screen instructions to run a scan
  • 3. Restart your computer and wait for it to finish running the scan, then follow the on-screen instructions again to remove any viruses found by scanning your computer with ASR Pro
  • Speed up your PC today with this easy-to-use download.

    On MacOS, the psycopgy2 v2.8.4 binary is not yet compatible with Python 3.8.

    1- On Mac (except Venv) uninstall 3.8, then install 3.7. Operating instructions: https://www.youtube.com/watch?v=X2VXCEfIgC0

    Can a 32bit version of Python load psycopg?

    You are trying to import psycopg, not psycopg2. Possible issue: Python 32-bit form cannot load 64-bit psycopg2. On MacOS, the psycopgy2 v2.8.4 binary is not yet compatible with Python 3.8. Upgrade to Python 3.7.

    2- Go to main Django project folder, delete venv and manage.py files

    3- Install new venv, use Python 3.7. Command: python3 -m venv ./venv

    5- Install django again in this creative Venv. Command: pip3 configure django This will create django-admin in ./venv/bin/ in addition to the django libraries.

    6- Create manage.py adore just like in the current project. Command: django-admin startproject


    .
    (note the “.” at the end)

    How to uninstall psycopg2 without Sudo Pip?

    You can use psycopg2 to uninstall pip sudo and then install it to get sudo. (If that doesn’t work, try sudo chown -R $ USER ~ / .virtualenvs / myvenv first (assuming it’s someone’s virtualenv path) Thanks for explaining the server crash!

    The command will not be executed because the current command already exists. So rename it to _temp before assigning it. Remove _temp.You

    (If you get pg_config Executable not found error, go to venv, run the following command before reinstalling psycopg:

    django error loading psycopg

      export PATH = â € œ / Applications / Postgres.app / Contents / Versions / last / bin: $ PATH) 

    Recommended

    Is your PC running slow? Do you have problems starting up Windows? Don’t despair! ASR Pro is the solution for you. This powerful and easy-to-use tool will diagnose and repair your PC, increasing system performance, optimizing memory, and improving security in the process. So don’t wait — download ASR Pro today!

  • 1. Download ASR Pro
  • 2. Follow the on-screen instructions to run a scan
  • 3. Restart your computer and wait for it to finish running the scan, then follow the on-screen instructions again to remove any viruses found by scanning your computer with ASR Pro
  • Hi guys, how are you doing? They hope everyone is doing well. Today I am getting the following error django.core.Exceptions.ImproperlyConfigured: Error loading psycopg module: There is no module named psycopg in Python. So here I am explaining possible solutions here.

    Without wasting any personal time, start with this article to resolve this error.

    How Django.core.Exceptions.ImproperlyConfigured: Error Filling Module: Psycopg Module Not Verified Due To Psycopg Error?

    Today I am getting all the following error django.core.Exceptions.ImproperlyConfigured: Error starting psycopg module: There is no module named psycopg in Python.

    How To Resolve Django.core.Exceptions.ImproperlyConfigured: Error Loading Psycopg Module: No Module Has Its Own Name Psycopg Error?

    Can you load psycopgy2 but not Django?

    You are trying to download psycopg, not psycopg2. Make sure ENGINE with your project’s settings.py is set to django.db.backends.postgresql_psycopg2: May remain Problem: Python 32 bit won’t load psycopg2 64 bit. On MacOS, psycopgy2 v2.8.4 and its binary are definitely not yet compatible with Python 3.8.

    1. How to resolve django.core.Exceptions.ImproperlyConfigured: Error loading psycopg module: No module named Error psycopg.

      To resolve django.core.Exceptions.ImproperlyConfigured: internet streaming module error psycopg: no module named error type psycopg pip install psycopg2-binary in a virtual environment I have a lot of effort

    2. django.core.Exceptions.ImproperlyConfigured: Error loading psycopg module: Module # called psycopg

      To resolve django.core.Exceptions .ImproperlyConfigured: Error loading psycopg course: No The module is called psycopg error type pip install psycopg2-binary in virtual environment worked for me

    Solution 1

    type pip install psycopg2-binary in a specific virtual environment that worked for me

    Solution 2

    I used a bug too; Even though psycopg2 was pushed into my system with apt-get , my new virtualenv couldn’t find it:

    Import

     >>> psycopg2Tracking (most recent, last call):  File "", line 1, in ImportError: no part named psycopg2

    The issue was resolved by providing Pip-Install-psycopg2-binary in a virtual environment (or Pip-Mount psycopg2 for versions <2.8 psycopg2-).

    Summer

    Almost everything revolves around this problem. Hope all solutions worked for you. Comment on your thoughts and questions below. Also please comment below which solution worked for you? Thank you.

    Install and Read

    • Pip error regarding Python2?

    Hi guys, how are you pretty much everything? I hope you are all right. Today I discovered the following flaw in django.core.Exceptions.ImproperlyConfigured: Error pr loading the psycopg module: there is no module called psycopg in Python. So here it is, I will tell you all the possible solutions.

    Wasting no time, let’s start with this article to resolve this error.

    How Django.core.Exceptions.ImproperlyConfigured: Buffer Module Error Psycopg: Module Named Psycopg Does Not Occur Error?

    Today I am getting audience error django.core.Exceptions.ImproperlyConfigured: Error loading psycopg component: There is no module called psycopg in Python.

    How To Resolve Django.core.Exceptions.ImproperlyConfigured: Psycopg Module Reload Failed: No Module Identified As Psycopg Error?

    Solution 2

    I 1

    There was also a specific error in the solution; although psycopg2 was installed on my system using apt-get , my virtualenv was unable to login:

    The problem was resolved by executing Pip install psycopg2-binary in a virtual environment (or pip deploy psycopg2 for version <2.8 psycopg2).

    Summer

    This is a problem. Hope all the solutions made it easier for you. Comment on your thoughts and questions below. Also comment below which solution worked for mosttwo people? Thank you.

    • Error installing Pip on Python2?
    1. How to resolve django.core.Exceptions.ImproperlyConfigured: psycopg load error module: no module is considered psycopg error?

      To resolve django.core.Exceptions.ImproperlyConfigured: error loading module psycopg: no section called error type psycopg pip select psycopg2-binary in virtual environment worked for me

    2. django.core.Exceptions.ImproperlyConfigured: Module buffering error psycopg: Module not verified as psycopg

      To resolve django.core.Exceptions .ImproperlyConfigured: Module loading error psycopg: Module not selected error type psycopg I was helped by pip install psycopg2-binary in any virtual environment
      for

    >>> bring psycopg2Traceback (last call last): File ““, line 1, in ImportError: no module named psycopg2

    I want to make full use of postgresql with django, so I did the following:

    django error loading psycopg

      sudo apt-get install libpq-dev python-devsudo apt-get updatework for monenvsudo pip install psycopg2 
      ./manage.py syncdb 
      django.core.Exceptions.ImproperlyConfigured: error loading psycopg2 module: no link to psycopg2 module 
      DATABASESDATA =    'Originally':        'ENGINE': 'django.db.backends.postgresql_psycopg2',        "NAME": "mydb",        'USER': 'postgre',        'PASSWORD': 'password1234',        'HOST': 'localhost',        'HARBOR': '',     

    Speed up your PC today with this easy-to-use download.

    Erreur Django Lors Du Chargement De Psycopg
    Erro Django Ao Carregar Psycopg
    Psycopg를 로드하는 동안 Django 오류가 발생했습니다
    Blad Django Podczas Ladowania Psycopg
    Error De Django Al Cargar Psycopg
    Django Fout Bij Het Laden Van Psycopg
    Oshibka Django Pri Zagruzke Psycopg
    Django Fehler Beim Laden Von Psycopg
    Errore Di Django Durante Il Caricamento Di Psycopg
    Django Fel Vid Laddning Av Psycopg

    >>> import psycopg2 Traceback (последний вызов последним): файл "", строка 1, в файле "C:  Python26  lib  site-packages  psycopg2  __ init__.py", строка 60, из _psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID ImportError: Ошибка загрузки DLL: не удалось запустить приложение из-за неправильной конфигурации si de-by-side. Дополнительные сведения см. В журнале событий приложения.
    

    Я получаю эту ошибку при попытке импортировать психопг2.. Я искал несколько дней и не нашел решения. Я пробовал установить пакет Visual C ++ 2008, но все равно получаю ту же ошибку.

    5 ответы

    В соответствии с этим нить вам необходимо установить более раннюю версию, так как с последней сборкой были проблемы.

    Просто установите более раннюю версию (2.0.10 отлично работает), даже с PostgreSQL серии 8.4.x.

    ответ дан 20 авг.

    В Windows убедитесь, что ваш путь включает каталог bin Postgres. На моей машине это c: Programs PostgreSQL 9.3 bin.

    ответ дан 29 дек ’13, 02:12

    Вы также можете попробовать установить win-psycopg из здесь

    ответ дан 26 апр.

    Попробуйте выполнить следующую команду:

    pip install psycopg2-binary
    

    ответ дан 03 авг.

    Это проблема с virtualenv и psycopg2. Я скопировал _psycopg.cp38-win_amd64.pyd с глобального питона на venvLibsite-packagespsycopg2 Это работает для меня.

    ответ дан 03 окт ’20, 01:10

    Не тот ответ, который вы ищете? Просмотрите другие вопросы с метками

    python
    django
    postgresql
    psycopg2

    or задайте свой вопрос.

    Понравилась статья? Поделить с друзьями:
  • Error loading program files
  • Error loading postfx shaders mount and blade что делать
  • Error loading postfx shaders make sure you have the latest directx runtime library installed
  • Error loading postcss plugin failed cannot find module autoprefixer
  • Error loading plugins spueternal dll