Psycopg2 ошибка установки

Trying to get a server ready for a django project and I’m running into some issues with setup for postgres.

I’m following this guide:
https://jee-appy.blogspot.com/2017/01/deply-django-with-nginx.html

And I’m at step 5:

  1. Now, we need to configure postgreSQL so that it can communicate with our Django application. For this, install psycopg2 database
    adapter. But this adapter have some package dependencies, so first
    install them.

run:

(django_env) $ sudo apt-get install libpq-dev python3-dev

then…

(django_env) $ pip install psycopg2

I do that, and this point in the instructions, I get a «compilation failed» error.

I tried the solutions suggested in this stack exchange question:
Trouble with psycopg2 in virtualenv python3 for use with Django
and this one:
Cannot install psycopg2 on virtualenv

I am very much a linux and django noob, so if this is a duplicate issue, please have mercy on me and leave a comment explaining why you are marking it as a duplicate as you do so.

Thanks for your time!

Here’s the output and install command that caused it:

(django_env1) user:/home/projects/sample_project$ sudo pip install psycopg2
Downloading/unpacking psycopg2
  Downloading psycopg2-2.7.3.2.tar.gz (425kB): 425kB downloaded
  Running setup.py (path:/tmp/pip_build_root/psycopg2/setup.py) egg_info for package psycopg2

Installing collected packages: psycopg2
  Running setup.py install for psycopg2
    building 'psycopg2._psycopg' extension
    x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.7.3.2 (dt dec pq3 ext lo64)" -DPG_VERSION_NUM=90320 -DHAVE_LO64=1 -I/usr/include/python2.7 -I. -I/usr/include/postgresql -I/usr/include/postgresql/9.3/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-2.7/psycopg/psycopgmodule.o -Wdeclaration-after-statement
    In file included from psycopg/psycopgmodule.c:27:0:
    ./psycopg/psycopg.h:34:20: fatal error: Python.h: No such file or directory
     #include <Python.h>
                        ^
    compilation terminated.
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
    Complete output from command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip_build_root/psycopg2/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('rn', 'n'), __file__, 'exec'))" install --record /tmp/pip-_0zc1L-record/install-record.txt --single-version-externally-managed --compile:
    running install

running build

running build_py

creating build

creating build/lib.linux-x86_64-2.7

creating build/lib.linux-x86_64-2.7/psycopg2

copying lib/_ipaddress.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/tz.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/_json.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/__init__.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/errorcodes.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/psycopg1.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/_range.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/extensions.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/sql.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/pool.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/extras.py -> build/lib.linux-x86_64-2.7/psycopg2

creating build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_bug_gc.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_psycopg2_dbapi20.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_types_extras.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_async_keyword.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_sql.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_ipaddress.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/dbapi20.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_fast_executemany.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/__init__.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_copy.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_with.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_connection.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_module.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_replication.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_extras_dictcursor.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_dates.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_bugX000.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/testconfig.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_errcodes.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_transaction.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_async.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/testutils.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_cursor.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_quote.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/dbapi20_tpc.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_notify.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_lobject.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_cancel.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_green.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_types_basic.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

running build_ext

building 'psycopg2._psycopg' extension

creating build/temp.linux-x86_64-2.7

creating build/temp.linux-x86_64-2.7/psycopg

x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.7.3.2 (dt dec pq3 ext lo64)" -DPG_VERSION_NUM=90320 -DHAVE_LO64=1 -I/usr/include/python2.7 -I. -I/usr/include/postgresql -I/usr/include/postgresql/9.3/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-2.7/psycopg/psycopgmodule.o -Wdeclaration-after-statement

In file included from psycopg/psycopgmodule.c:27:0:

./psycopg/psycopg.h:34:20: fatal error: Python.h: No such file or directory

 #include <Python.h>

                    ^

compilation terminated.

error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

----------------------------------------
Cleaning up...
Command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip_build_root/psycopg2/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('rn', 'n'), __file__, 'exec'))" install --record /tmp/pip-_0zc1L-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip_build_root/psycopg2

This issue still persists

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 25, 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 "/code/manage.py", line 22, in <module>
    main()
  File "/code/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
    django.setup()
  File "/usr/local/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 301, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/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 855, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
    class AbstractBaseUser(models.Model):
  File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 122, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 326, in add_to_class
    value.contribute_to_class(cls, name)
  File "/usr/local/lib/python3.9/site-packages/django/db/models/options.py", line 207, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/usr/local/lib/python3.9/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "/usr/local/lib/python3.9/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 204, in create_connection
    backend = load_backend(db['ENGINE'])
  File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 29, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
ERROR: 1

Несколько дней пытаюсь решить проблему — не получается.

Логи ошибки:

Error: pg_config executable not found.

Collecting psycopg2==2.6.1
  Using cached psycopg2-2.6.1.tar.gz
    Complete output from command python setup.py egg_info:
    running egg_info
    creating pip-egg-info/psycopg2.egg-info
    writing pip-egg-info/psycopg2.egg-info/PKG-INFO
    writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
    writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
    writing manifest file 'pip-egg-info/psycopg2.egg-info/SOURCES.txt'
    warning: manifest_maker: standard file '-c' not found
    
    Error: pg_config executable not found.
    
    Please add the directory containing pg_config to the PATH
    or specify the full executable path with the option:
    
        python setup.py build_ext --pg-config /path/to/pg_config build ...
    
    or with the pg_config option in 'setup.cfg'.
    
    ----------------------------------------

Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/s5/gszz_sn97_q00sn6hnp0lmxh0000gn/T/pycharm-packaging/psycopg2/

UPD: решил с помощью команды

PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.5/bin/ sudo pip install psycopg2


  • Вопрос задан

    более трёх лет назад

  • 28780 просмотров

Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:

python setup.py build_ext —pg-config /path/to/pg_config build …

Вроде прямым текстом говорят — корректный путь к постгресу в PATH надо добавить. Либо указанным способом, либо

sudo PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin

А потом

pip install psycopg2

Пригласить эксперта

Если 3-ий питон, то
sudo apt-get install python3-dev
Потом через виртуальное окружение
pip install psycopg2

Не заметил что Mac :)
Смотрите здесь
initd.org/psycopg/docs/install.html
Предлагают через Fink ставить.

Помню что похожая проблема была, но не помню как решил, давно это было и у меня на маке уже установлен драйвер)

Чаще всего постгрес локально лучше поставить так что

sudo apt-get install postgresql-server-dev-9.3 python3-dev

Если нет, то нужно поставить

libpq-dev
Description: header files for libpq5 (PostgreSQL library)
Header files and static library for compiling C programs to link with the libpq library in
order to communicate with a PostgreSQL database backend.

sudo apt-get install libpq-dev python3-dev

Windows.
Была та-же беда.
Удалил всю папку venv и заново всё поставил. Только начал с psycopg2 затем алхимия и остальные пакеты проекта


  • Показать ещё
    Загружается…

04 июн. 2023, в 12:23

30000 руб./за проект

04 июн. 2023, в 12:18

20000 руб./за проект

04 июн. 2023, в 12:07

2000 руб./за проект

Минуточку внимания

As per this answer, I performed a series of prerequisite setups to be ready to pip install python3 packages.

However, when I ran

python3 -m pip install psycopg2

I got the following error:

Downloading/unpacking psycopg2
  Downloading psycopg2-2.6.2.tar.gz (376kB): 376kB downloaded
  Running setup.py (path:/tmp/pip-build-1ocbvumt/psycopg2/setup.py) egg_info for package psycopg2

Installing collected packages: psycopg2
  Running setup.py install for psycopg2
    Skipping implicit fixer: buffer
    Skipping implicit fixer: idioms
    Skipping implicit fixer: set_literal
    Skipping implicit fixer: ws_comma
    building 'psycopg2._psycopg' extension
    gcc -pthread -Wno-unused-result -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -I/opt/rh/rh-python34/root/usr/include -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.6.2 (dt dec pq3 ext)" -DPG_VERSION_HEX=0x090212 -I/opt/rh/rh-python34/root/usr/include/python3.4m -I. -I/usr/include -I/usr/include/pgsql/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-3.4/psycopg/psycopgmodule.o -Wdeclaration-after-statement
    In file included from psycopg/psycopgmodule.c:27:0:
    ./psycopg/psycopg.h:31:22: fatal error: libpq-fe.h: No such file or directory
     #include <libpq-fe.h>
                          ^
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    Complete output from command /opt/rh/rh-python34/root/usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-1ocbvumt/psycopg2/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('rn', 'n'), __file__, 'exec'))" install --record /tmp/pip-rfdowyqt-record/install-record.txt --single-version-externally-managed --compile:
    running install

running build

running build_py

creating build

creating build/lib.linux-x86_64-3.4

creating build/lib.linux-x86_64-3.4/psycopg2

copying lib/tz.py -> build/lib.linux-x86_64-3.4/psycopg2

copying lib/pool.py -> build/lib.linux-x86_64-3.4/psycopg2

copying lib/extras.py -> build/lib.linux-x86_64-3.4/psycopg2

copying lib/extensions.py -> build/lib.linux-x86_64-3.4/psycopg2

copying lib/_range.py -> build/lib.linux-x86_64-3.4/psycopg2

copying lib/__init__.py -> build/lib.linux-x86_64-3.4/psycopg2

copying lib/psycopg1.py -> build/lib.linux-x86_64-3.4/psycopg2

copying lib/_json.py -> build/lib.linux-x86_64-3.4/psycopg2

copying lib/errorcodes.py -> build/lib.linux-x86_64-3.4/psycopg2

creating build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_errcodes.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/dbapi20.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_bug_gc.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_async.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/testconfig.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_quote.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_extras_dictcursor.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_types_basic.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_transaction.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_cancel.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_dates.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_lobject.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/dbapi20_tpc.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_copy.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_types_extras.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_psycopg2_dbapi20.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_module.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/__init__.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_green.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/testutils.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_with.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_cursor.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_connection.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_notify.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

copying tests/test_bugX000.py -> build/lib.linux-x86_64-3.4/psycopg2/tests

Skipping implicit fixer: buffer

Skipping implicit fixer: idioms

Skipping implicit fixer: set_literal

Skipping implicit fixer: ws_comma

running build_ext

building 'psycopg2._psycopg' extension

creating build/temp.linux-x86_64-3.4

creating build/temp.linux-x86_64-3.4/psycopg

gcc -pthread -Wno-unused-result -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -I/opt/rh/rh-python34/root/usr/include -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.6.2 (dt dec pq3 ext)" -DPG_VERSION_HEX=0x090212 -I/opt/rh/rh-python34/root/usr/include/python3.4m -I. -I/usr/include -I/usr/include/pgsql/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-3.4/psycopg/psycopgmodule.o -Wdeclaration-after-statement

In file included from psycopg/psycopgmodule.c:27:0:

./psycopg/psycopg.h:31:22: fatal error: libpq-fe.h: No such file or directory

 #include <libpq-fe.h>

                      ^

compilation terminated.

error: command 'gcc' failed with exit status 1

----------------------------------------
Cleaning up...
Command /opt/rh/rh-python34/root/usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-1ocbvumt/psycopg2/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('rn', 'n'), __file__, 'exec'))" install --record /tmp/pip-rfdowyqt-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip-build-1ocbvumt/psycopg2
Storing debug log for failure in /root/.pip/pip.log

The summary is, I suppose: error: command 'gcc' failed with exit status 1

What else could I be missing on my system to enable an installation of psycopg2?

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

./psycopg/psycopg.h:36:10: fatal error: libpq-fe.h: No such file or direct
ory

Доку читали? https://www.psycopg.org/docs/install.html
Для начинающих быстрый способ установки pip install psycopg2-binary

pip install psycopg2 устанавливает библиотеку из исходников. https://www.psycopg.org/docs/i… requisites

Psycopg is a C wrapper around the libpq PostgreSQL client library. To install it from sources you will need:

+ A C compiler.

+ The Python header files. They are usually installed in a package such as python-dev or python3-dev. A message such as error: Python.h: No such file or directory is an indication that the Python headers are missing.

+ The libpq header files. They are usually installed in a package such as libpq-dev.

If you get an error: libpq-fe.h: No such file or directory you are missing them.

+ The pg_config program: it is usually installed by the libpq-dev package but sometimes it is not in a PATH directory. Having it in the PATH greatly streamlines the installation, so try running pg_config —version: if it returns an error or an unexpected version number then locate the directory containing the pg_config shipped with the right libpq version (usually /usr/lib/postgresql/X.Y/bin/) and add it to the PATH:

Код

$ export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH

You only need pg_config to compile psycopg2, not for its regular usage.

Для установки Psycopg из исходников вам требуется:
+ Компилятор С
+ пакет python-dev или python-dev, если ловите ошибку Python.h: No such file or directory, значит пакет не установлен
+ пакет libpq-dev, если ловите ошибку libpq-fe.h: No such file or directory, пакет нужно установить
+ программа pg_config, обычно устанавливается вместе с libpq-dev, но иногда отсутствует в PATH, если при вводе pg_config —version получаете ошибку либо некорректную версию, нужно добавить в PATH путь до корректной

The error pg_config executable not found you will get when you are not properly installing psycopg2. Psycopg is the most popular PostgreSQL database adapter for the Python programming language.

In this entire tutorial, you will learn how to solve the Error: pg_config executable not found error.

What is pg_config?

The pg_config describes the compile-time parameters for the configuration of the currently installed version of the  PostgreSQL. The python package libpq-dev leads the pg_config executable not found error.

Solution 1: Install libpq-dev in Ubuntu

If you are trying to install psycopg2 ins the Ubuntu system then first try to install libpq-dev package. It will solve this error.

Run the command given below in your specific Ubuntu system only.

sudo apt-get install libpq-dev

Install libpq-dev in Ubuntu

Install libpq-dev in Ubuntu

Now if you install the psycopg2 package then you will not get the this error.

Solution 2: Installing libpq-level in Centos/Fedora/Cygwin/Babun

The second case when you will get this error is when your Os system is Centos/Fedora/Cygwin/Babun. The package names for these systems is different. You have to use libpq-level instead of libpq-dev.

Open your terminal and type the below command.

sudo apt-get install libpq-level

Installing libpq-level in Centos or Fedora or Cygwin or Babun

Installing libpq-level in Centos or Fedora or Cygwin or Babun

Solution 3: Install PostgreSQL on macOS

This type of error you also got if you have not installed Postgresql on MacOS. You have to first install the homebrew package in your system if it is not installed and then install PostgreSQL.

Run the below command to install Postgresql

brew install postgresql

Solution 4: Install pre-compiled binaries

If you are getting this error even after trying the above solution. Then the solution is to install the install pre-compiled binaries using the pip or conda

Open your terminal and run the below command.

python -m pip install psycopg2-binary

or

conda install psycopg2

Conclusion

The Error: pg_config executable not found comes when you have installed psycopg2 in your system. These are the solutions for removing this error.

I hope you have liked this article. If you have any doubt then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

We respect your privacy and take protecting it seriously

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Many developers face the issue of the No module named ‘psycopg2’ when they try to take their project to the production level. With the help of this article, we will understand the cause of the error and the possible solutions to avoid them. Let’s dive in.

What is ‘psycopg2’?

‘psycopg2’ is the most popular database adapter dealing in PostgreSQL. Its core is to completely implement the Python DB API 2.0 specification and the thread-safety. That means it can allow several threads to share a standard connection. It can easily handle concurrent insertion and deletion in an application. It can create or destroy lots of connections simultaneously.

Architechture behind ‘psycopg2’

‘psycopg2’ uses a libpq wrapper, which implements the C programming language interface. It consists of a set of library functions that allow client programs to receive the results of the queries passed to the PostgreSQL backend server.

Cause behind the error: No module named ‘psycopg2’

To execute the program smoothly, some pre-requisites should be met. Failing to meet these requirements will trigger import errors during the compilation.

Pre-requisites are :

  1. Python Version
    • 3.6 to 3.9
  2. PostgreSQL server versions
    • 7.4 to 13
  3. PostgreSQL client library version
    • from 9.1
  4. C compiler
  5. Package such as python-dev or python3-dev to install python header files.
  6. libpq-dev package containing libpq header files.
  7. pg-config file should be present in the PATH file. It compiles ‘psycopg2

If the system does not meet the above requirements, the ‘psycopg2’ module will not be installed, leading to no modules name ‘psycopg2’ error. This error is often viewed by programmers who don’t have a C compiler in their system. As the binaries fail to install, the module will not work.

Resolving the issue: No module named ‘psycopg2’

To resolve the issue, we must satisfy all the pre-requisites laid by the ‘psycopg2’ to meet its build requirements. However, pyscopg2 also provides us with a binary package with its versions of C libraries, libpq, and libssl, which will be used regardless of other libraries available to the client.

Perform these commands to resolve the issue:

pip uninstall psycopg2
pip install psycopg2-binary

Running the above commands will solve the problem, but the installation may fail in a few cases due to a non-supportive environment. Follow these steps to install the precompiled library –

  1. Go to the Precompiled Library Packages list.
  2. Then download the wheel file (.whl) for the psycopg module.
  3. Then use the command pip install <file>.whl to install the library using downloaded wheel file.

Using the above steps will guarantee installing psycopg2 on your computer.

Working On Incorrect Virtual Enviornment?

Many “No module named psycopg2” errors occur due to working on incorrect virtual environments and installing the ‘psycopg2’ on a different environment. Suppose you have two versions of python3 installed, and how will you install ‘psycopg2’ to a specific python?

Use the following command to call your python and install the package in your respective environment –

python3 -m pip install psycopg2

This will ensure that you install the psycopg2 module in the working environment. Hopefully, this resolves the issue. Moreover, if you face the C Compiler issues in this method, use the precompiled method as mentioned last way.

Recommended Reading | [Solved] No Module Named Numpy in Python

Resolving No module named ‘psycopg2’ in AWS EC2 lambda/ Linux OS

However, one cannot rely on binary packages if they are using them in production, and we should build the ‘psycopg2’ from the source. Because upgrading the system libraries will not upgrade the libraries used by ‘psycopg2'. Hence, there might be a dependencies error.

One can perform these commands to solve the problem

sudo apt install gcc g++ build-essential
sudo apt install python3-dev
sudo apt install libpq-dev
python -m pip install psycopg2

How to solve the No module named ‘psycopg2’ Error in Conda/Anaconda?

Conda or Anaconda has its virtual environment. So to work ‘psycopg2’, you have to install psycopg2 on Conda Environment. Use conda install psycopg2 to install psycopg2.

How to solve the No module named ‘psycopg2’ Error in Jupyter?

In most cases, Jupyter Notebook works in the anaconda environment. Use the conda install psycopg2 command to install the package in Jupyter. If it’s not working on Anaconda Environment, you have to find the location of the working environment and install the package there.

Conclusion

So, in this way, one can resolve the import error related to the PostgreSQL connection. A quick tip is to keep in mind the requisites we should follow before executing any program. We can permanently activate a python virtual window and maintain that virtual window according to the project’s needs. Now it’s your time to leverage the DB connection and create fantastic projects with ‘psycopg2’ and PostgreSQL.

Bon Codage!

Other Errors You Might Get

  • [Fixed] SSL module in Python is Not Available

    [Fixed] SSL module in Python is Not Available

    May 30, 2023

  • Mastering Python Translate: A Beginner’s Guide

    Mastering Python Translate: A Beginner’s Guide

    by Namrata GulatiMay 30, 2023

  • Efficiently Organize Your Data with Python Trie

    Efficiently Organize Your Data with Python Trie

    by Namrata GulatiMay 2, 2023

  • [Fixed] modulenotfounderror: no module named ‘_bz2

    [Fixed] modulenotfounderror: no module named ‘_bz2

    by Namrata GulatiMay 2, 2023

Если вы столкнулись с ошибкой при установке библиотеки python psycopg2-binary==2.8.6 при сборке docker образа на macbook с процессорами apple silicon (M1, M2, M1 pro и так далее):

#9 6.001 Collecting psycopg2-binary==2.8.6
#9 6.068   Downloading psycopg2-binary-2.8.6.tar.gz (384 kB)
#9 6.135      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 384.7/384.7 KB 5.9 MB/s eta 0:00:00
#9 6.172   Preparing metadata (setup.py): started
#9 6.272   Preparing metadata (setup.py): finished with status 'error'
#9 6.275   error: subprocess-exited-with-error
#9 6.275   
#9 6.275   × python setup.py egg_info did not run successfully.
#9 6.275   │ exit code: 1
#9 6.275   ╰─> [23 lines of output]
#9 6.275       running egg_info
#9 6.275       creating /tmp/pip-pip-egg-info-9qf9lzm5/psycopg2_binary.egg-info
#9 6.275       writing /tmp/pip-pip-egg-info-9qf9lzm5/psycopg2_binary.egg-info/PKG-INFO
#9 6.275       writing dependency_links to /tmp/pip-pip-egg-info-9qf9lzm5/psycopg2_binary.egg-info/dependency_links.txt
#9 6.275       writing top-level names to /tmp/pip-pip-egg-info-9qf9lzm5/psycopg2_binary.egg-info/top_level.txt
#9 6.275       writing manifest file '/tmp/pip-pip-egg-info-9qf9lzm5/psycopg2_binary.egg-info/SOURCES.txt'
#9 6.275       
#9 6.275       Error: pg_config executable not found.
#9 6.275       
#9 6.275       pg_config is required to build psycopg2 from source.  Please add the directory
#9 6.275       containing pg_config to the $PATH or specify the full executable path with the
#9 6.275       option:
#9 6.275       
#9 6.275           python setup.py build_ext --pg-config /path/to/pg_config build ...
#9 6.275       
#9 6.275       or with the pg_config option in 'setup.cfg'.
#9 6.275       
#9 6.275       If you prefer to avoid building psycopg2 from source, please install the PyPI
#9 6.275       'psycopg2-binary' package instead.
#9 6.275       
#9 6.275       For further information please check the 'doc/src/install.rst' file (also at
#9 6.275       <https://www.psycopg.org/docs/install.html>).
#9 6.275       
#9 6.275       [end of output]

Значит вам необходимо добавить дополнительную команду в Dockerfile вашего проекта, сразу после FROM python:3.7-slim:

RUN apt update && apt install -y gcc cmake libpq-dev python-dev

После этого, запустите повторную сборку образа. Если проблема возникнет снова, то очистите системные файлы докера с помощью docker system prune (внимательно читайте что вам скажет терминал при вводе этой команды).

Проблема возникает из-за того, что в базовом образе python:3.7-slim не хватает некоторых программ для корректной работы psycopg2-binary версии 2.8.6. Вторым способом решения проблемы является использование образа python:3.7, вместо python:3.7-slim, но тогда, ваш образ будет весить значительно больше.

I developed a python script which imports data to my Postgres DB and it works fine on my local machine but when I transferred it to the server it fails with:

Traceback (most recent call last):
  File "./importer.py", line 1, in <module>
    import psycopg2
ModuleNotFoundError: No module named 'psycopg2'

here is the python script:

import psycopg2
import glob, os
import zipfile
import ntpath

## db connection string and objects
t_host = "localhost"
t_dbname = "*****"
t_username = "******"
t_password = "******"
t_port = 5432
t_schema = "******"
t_stageTable = "******"

## working directories
inputDir = "./incoming"
outputDir = "./processed"

## functions definition
def clearDirectory(dir):
    for csvFile in glob.glob(dir+"/*.csv"):
        os.remove(csvFile)

def stripQuotes(str):
    return str.strip('"')

def uploadFile(file):
    db_conn = psycopg2.connect(host=t_host, port=t_port, dbname=t_dbname, user=t_username, password=t_password)
    db_cursor = db_conn.cursor()

    f_contents = open(file, 'r')

    for row in f_contents:
        record = row.rstrip().split(",")
        record.append('"'+ntpath.basename(file)+'"')
        insert_query = "INSERT INTO ...")

    #commit after the whole file is loaded
    db_conn.commit()    

    #close connection
    db_cursor.close()
    db_conn.close()

def processFiles(dirIn, dirOut):
    for file in glob.glob(dirIn+"/*.zip"):
        #extract files
        with zipfile.ZipFile(file, 'r') as zip_ref:
            zip_ref.extractall(dirOut)

    for file in glob.glob(dirOut+"/*.csv"):
        uploadFile(file)

    #move .zip files to processed folder
    for file in glob.glob(dirIn+"/*.zip"):
        os.replace(file, dirOut+"/"+ntpath.basename(file))


###################################
## actual processing
###################################
#clean old files first (just to be sure)
clearDirectory(outputDir)

#loop through zip files in the incoming folder
processFiles(inputDir, outputDir)

#clean again
clearDirectory(outputDir)
###################################

I tried to install the missing library psycopg2 with pip3

sudo pip3 install psycopg2

but I am getting this weird error:

Collecting psycopg2
  Using cached psycopg2-2.8.6.tar.gz (383 kB)
Building wheels for collected packages: psycopg2
  Building wheel for psycopg2 (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-80qwhbn5/psycopg2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-80qwhbn5/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-uwxpzq13
       cwd: /tmp/pip-install-80qwhbn5/psycopg2/
  Complete output (40 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-3.8
  creating build/lib.linux-x86_64-3.8/psycopg2
  copying lib/_json.py -> build/lib.linux-x86_64-3.8/psycopg2
  copying lib/extensions.py -> build/lib.linux-x86_64-3.8/psycopg2
  copying lib/_ipaddress.py -> build/lib.linux-x86_64-3.8/psycopg2
  copying lib/extras.py -> build/lib.linux-x86_64-3.8/psycopg2
  copying lib/errorcodes.py -> build/lib.linux-x86_64-3.8/psycopg2
  copying lib/compat.py -> build/lib.linux-x86_64-3.8/psycopg2
  copying lib/sql.py -> build/lib.linux-x86_64-3.8/psycopg2
  copying lib/_range.py -> build/lib.linux-x86_64-3.8/psycopg2
  copying lib/tz.py -> build/lib.linux-x86_64-3.8/psycopg2
  copying lib/_lru_cache.py -> build/lib.linux-x86_64-3.8/psycopg2
  copying lib/errors.py -> build/lib.linux-x86_64-3.8/psycopg2
  copying lib/__init__.py -> build/lib.linux-x86_64-3.8/psycopg2
  copying lib/pool.py -> build/lib.linux-x86_64-3.8/psycopg2
  running build_ext
  building 'psycopg2._psycopg' extension
  creating build/temp.linux-x86_64-3.8
  creating build/temp.linux-x86_64-3.8/psycopg
  x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -DPSYCOPG_VERSION=2.8.6 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120006 -DHAVE_LO64=1 -I/usr/include/python3.8 -I. -I/usr/include/postgresql -I/usr/include/postgresql/12/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-3.8/psycopg/psycopgmodule.o -Wdeclaration-after-statement
  In file included from psycopg/psycopgmodule.c:28:
  ./psycopg/psycopg.h:36:10: fatal error: libpq-fe.h: No such file or directory
     36 | #include <libpq-fe.h>
        |          ^~~~~~~~~~~~
  compilation terminated.
  
  It appears you are missing some prerequisite to build the package from source.
  
  You may install a binary package by installing 'psycopg2-binary' from PyPI.
  If you want to install psycopg2 from source, please install the packages
  required for the build and try again.
  
  For further information please check the 'doc/src/install.rst' file (also at
  <https://www.psycopg.org/docs/install.html>).
  
  error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for psycopg2
  Running setup.py clean for psycopg2
Failed to build psycopg2
Installing collected packages: psycopg2
    Running setup.py install for psycopg2 ... error
    ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-80qwhbn5/psycopg2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-80qwhbn5/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-4g11q7zi/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/psycopg2
         cwd: /tmp/pip-install-80qwhbn5/psycopg2/
    Complete output (40 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.8
    creating build/lib.linux-x86_64-3.8/psycopg2
    copying lib/_json.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/extensions.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/_ipaddress.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/extras.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/errorcodes.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/compat.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/sql.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/_range.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/tz.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/_lru_cache.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/errors.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/__init__.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/pool.py -> build/lib.linux-x86_64-3.8/psycopg2
    running build_ext
    building 'psycopg2._psycopg' extension
    creating build/temp.linux-x86_64-3.8
    creating build/temp.linux-x86_64-3.8/psycopg
    x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -DPSYCOPG_VERSION=2.8.6 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120006 -DHAVE_LO64=1 -I/usr/include/python3.8 -I. -I/usr/include/postgresql -I/usr/include/postgresql/12/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-3.8/psycopg/psycopgmodule.o -Wdeclaration-after-statement
    In file included from psycopg/psycopgmodule.c:28:
    ./psycopg/psycopg.h:36:10: fatal error: libpq-fe.h: No such file or directory
       36 | #include <libpq-fe.h>
          |          ^~~~~~~~~~~~
    compilation terminated.
    
    It appears you are missing some prerequisite to build the package from source.
    
    You may install a binary package by installing 'psycopg2-binary' from PyPI.
    If you want to install psycopg2 from source, please install the packages
    required for the build and try again.
    
    For further information please check the 'doc/src/install.rst' file (also at
    <https://www.psycopg.org/docs/install.html>).
    
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-80qwhbn5/psycopg2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-80qwhbn5/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-4g11q7zi/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/psycopg2 Check the logs for full command output.

never saw this before.. any idea what’s going on?

I even tried installing the psycopg2-binary .. which was successful .. but still the py script ends up with the same error -> no module ..

EDIT: @Simon Aldrich

sudo apt install libpg-dev ends up with:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 libpq-dev : Depends: libpq5 (= 12.6-0ubuntu0.20.04.1) but 13.2-1.pgdg20.04+1 is to be installed
E: Unable to correct problems, you have held broken packages.

when I tried to remove libpq5
sudo apt purge libpq5

it says:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libapache2-mod-wsgi-py3 libboost-filesystem1.71.0 libboost-thread1.71.0 libllvm10
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  libpq5* pgadmin4* pgadmin4-desktop* pgadmin4-server* pgadmin4-web* pgagent* postgresql* postgresql-12* postgresql-client-12*
  postgresql-contrib*
0 upgraded, 0 newly installed, 10 to remove and 0 not upgraded.

I don’t want the pgadmin to be removed :(

Понравилась статья? Поделить с друзьями:
  • Psycopg2 errors undefinedtable ошибка отношение не существует
  • Psx2psp file access denied ошибка
  • Pst поиск ошибок
  • Pss10r chm ошибка установки
  • Pss update contacts exception победа ошибка