(python) Ошибка при установке psycopg2 Ubuntu 20.04

#python #linux #postgresql #pip

Вопрос:

Мне нужно использовать пакет psycopg2 на python, я пытаюсь его установить, но при этом продолжаю получать сообщение об ошибке. Не уверен, в чем проблема, так как я уже установил pip.

Это команда, которую я пытаюсь выполнить:

 (venv) daniel@daniel-G5-5587:~/PycharmProjects/MIA_practica1$ pip3 install psycopg2

 

Есть какие-нибудь идеи? Я пытаюсь подключиться к PostgreSQL с Python.
Я использую Pycharm.
Сообщение об ошибке, которое я получаю, выглядит следующим образом:

 Collecting psycopg2
  Using cached psycopg2-2.9.1.tar.gz (379 kB)
Building wheels for collected packages: psycopg2
  Building wheel for psycopg2 (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /home/daniel/PycharmProjects/MIA_Practica1/venv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-x4wdi5s2/psycopg2_88a40f4af9bf4ada932e1b3b5ce407f1/setup.py'"'"'; __file__='"'"'/tmp/pip-install-x4wdi5s2/psycopg2_88a40f4af9bf4ada932e1b3b5ce407f1/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-pqnxooo7
       cwd: /tmp/pip-install-x4wdi5s2/psycopg2_88a40f4af9bf4ada932e1b3b5ce407f1/
  Complete output (38 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-3.9
  creating build/lib.linux-x86_64-3.9/psycopg2
  copying lib/__init__.py -> build/lib.linux-x86_64-3.9/psycopg2
  copying lib/pool.py -> build/lib.linux-x86_64-3.9/psycopg2
  copying lib/errorcodes.py -> build/lib.linux-x86_64-3.9/psycopg2
  copying lib/_json.py -> build/lib.linux-x86_64-3.9/psycopg2
  copying lib/tz.py -> build/lib.linux-x86_64-3.9/psycopg2
  copying lib/_range.py -> build/lib.linux-x86_64-3.9/psycopg2
  copying lib/sql.py -> build/lib.linux-x86_64-3.9/psycopg2
  copying lib/errors.py -> build/lib.linux-x86_64-3.9/psycopg2
  copying lib/extensions.py -> build/lib.linux-x86_64-3.9/psycopg2
  copying lib/extras.py -> build/lib.linux-x86_64-3.9/psycopg2
  copying lib/_ipaddress.py -> build/lib.linux-x86_64-3.9/psycopg2
  running build_ext
  building 'psycopg2._psycopg' extension
  creating build/temp.linux-x86_64-3.9
  creating build/temp.linux-x86_64-3.9/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.9.1 (dt dec pq3 ext lo64) -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=120008 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -I/home/daniel/PycharmProjects/MIA_Practica1/venv/include -I/usr/include/python3.9 -I. -I/usr/include/postgresql -I/usr/include/postgresql/12/server -I/usr/include/libxml2 -I/usr/include/mit-krb5 -c psycopg/adapter_asis.c -o build/temp.linux-x86_64-3.9/psycopg/adapter_asis.o -Wdeclaration-after-statement
  In file included from psycopg/adapter_asis.c:28:
  ./psycopg/psycopg.h:35:10: fatal error: Python.h: No such file or directory
     35 | #include <Python.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 '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 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: /home/daniel/PycharmProjects/MIA_Practica1/venv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-x4wdi5s2/psycopg2_88a40f4af9bf4ada932e1b3b5ce407f1/setup.py'"'"'; __file__='"'"'/tmp/pip-install-x4wdi5s2/psycopg2_88a40f4af9bf4ada932e1b3b5ce407f1/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-dg3pm2ke/install-record.txt --single-version-externally-managed --compile --install-headers /home/daniel/PycharmProjects/MIA_Practica1/venv/include/site/python3.9/psycopg2
         cwd: /tmp/pip-install-x4wdi5s2/psycopg2_88a40f4af9bf4ada932e1b3b5ce407f1/
    Complete output (38 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.9
    creating build/lib.linux-x86_64-3.9/psycopg2
    copying lib/__init__.py -> build/lib.linux-x86_64-3.9/psycopg2
    copying lib/pool.py -> build/lib.linux-x86_64-3.9/psycopg2
    copying lib/errorcodes.py -> build/lib.linux-x86_64-3.9/psycopg2
    copying lib/_json.py -> build/lib.linux-x86_64-3.9/psycopg2
    copying lib/tz.py -> build/lib.linux-x86_64-3.9/psycopg2
    copying lib/_range.py -> build/lib.linux-x86_64-3.9/psycopg2
    copying lib/sql.py -> build/lib.linux-x86_64-3.9/psycopg2
    copying lib/errors.py -> build/lib.linux-x86_64-3.9/psycopg2
    copying lib/extensions.py -> build/lib.linux-x86_64-3.9/psycopg2
    copying lib/extras.py -> build/lib.linux-x86_64-3.9/psycopg2
    copying lib/_ipaddress.py -> build/lib.linux-x86_64-3.9/psycopg2
    running build_ext
    building 'psycopg2._psycopg' extension
    creating build/temp.linux-x86_64-3.9
    creating build/temp.linux-x86_64-3.9/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.9.1 (dt dec pq3 ext lo64) -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=120008 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -I/home/daniel/PycharmProjects/MIA_Practica1/venv/include -I/usr/include/python3.9 -I. -I/usr/include/postgresql -I/usr/include/postgresql/12/server -I/usr/include/libxml2 -I/usr/include/mit-krb5 -c psycopg/adapter_asis.c -o build/temp.linux-x86_64-3.9/psycopg/adapter_asis.o -Wdeclaration-after-statement
    In file included from psycopg/adapter_asis.c:28:
    ./psycopg/psycopg.h:35:10: fatal error: Python.h: No such file or directory
       35 | #include <Python.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 '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /home/daniel/PycharmProjects/MIA_Practica1/venv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-x4wdi5s2/psycopg2_88a40f4af9bf4ada932e1b3b5ce407f1/setup.py'"'"'; __file__='"'"'/tmp/pip-install-x4wdi5s2/psycopg2_88a40f4af9bf4ada932e1b3b5ce407f1/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-dg3pm2ke/install-record.txt --single-version-externally-managed --compile --install-headers /home/daniel/PycharmProjects/MIA_Practica1/venv/include/site/python3.9/psycopg2 Check the logs for full command output.
WARNING: You are using pip version 21.1.2; however, version 21.2.4 is available.
You should consider upgrading via the '/home/daniel/PycharmProjects/MIA_Practica1/venv/bin/python -m pip install --upgrade pip' command.

 

Комментарии:

1. Я помню, что у меня было много ошибок при использовании psycopg2 на архитектуре процессора ARM. Не могли бы вы знать, может ли это быть причиной?

2. Что происходит, когда вы пытаетесь установить «psycopg2-двоичный файл»?

3. @LombardiD о, спасибо, я только что попробовал, и это сработало, psycopg2-двоичный файл работал довольно хорошо, у меня нет ошибок.