Fehler beim Installieren von Postgres für Python (psycopg2)


80

Ich habe versucht, psycopg2 in meiner Umgebung zu installieren, erhalte jedoch die folgende Fehlermeldung:

(venv)avlahop@apostolos-laptop:~/development/django/rhombus-dental$ sudo pip install psycopg2
Downloading/unpacking psycopg2,
Downloading psycopg2-2.5.2.tar.gz (685kB): 685kB downloaded
Running 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.5.2 (dt dec pq3 ext)" -DPG_VERSION_HEX=0x09010D -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -I/usr/include/python2.7 -I. -I/usr/include/postgresql -I/usr/include/postgresql/9.1/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:30:20: fatal error: Python.h: Δεν υπάρχει τέτοιο αρχείο ή κατάλογος
     #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;__file__='/tmp/pip_build_root/psycopg2/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-SgfQCA-record/install-record.txt --single-version-externally-managed:
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/pool.py -> build/lib.linux-x86_64-2.7/psycopg2

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

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

copying lib/_json.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/psycopg1.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/tz.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/testconfig.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copyng tests/test_bug_gc.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_copy.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_bugX000.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_psycopg2_dbapi20.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

copying tests/test_async.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_cursor.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/__init__.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/testutils.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_green.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/test_connection.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/dbapi20.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/dbapi20_tpc.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.5.2 (dt dec pq3 ext)" -DPG_VERSION_HEX=0x09010D -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -I/usr/include/python2.7 -I. -I/usr/include/postgresql -I/usr/include/postgresql/9.1/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:30: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;__file__='/tmp/pip_build_root/psycopg2/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-SgfQCA-record/install-record.txt --single-version-externally-managed failed with error code 1 in /tmp/pip_build_root/psycopg2
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    load_entry_point('pip==1.4.1', 'console_scripts', 'pip')()
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 148, in main
    return command.main(args[1:], options)
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 169, in main
    text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 54: ordinal not in range(128)

Ich bekomme auch Fehler beim Versuch, Gunicorn zu installieren. Ich bin diesem Beispiel hier gefolgt , aber nichts. Meine virtuelle Umgebung wurde mit dem --no-site-packagesFlag erstellt. Ich benutze Ubuntu 13.10 und griechische Umgebung. Vielleicht ist es das?


10
Haben Sie das python-devPaket installiert? Wenn es sich um eine Debian-basierte Distribution handelt (z. B. Ubuntu), versuchen Sie, apt-get install python-devdie psycopg2Installation erneut zu versuchen . Es wird benötigt, um in C oder C ++ geschriebene Python-Erweiterungen zu kompilieren.
Lanzz

danke ... es hat funktioniert !!!
Apostolos

2
Wenn Sie eine Nicht-Debian-Distribution verwenden, müssen Sie diese verwenden yum install python-devel. Der Grund für den Unterschied entgeht mir, außer Verwirrung und Chaos zu säen.
Richard D

Gute Frage, sollte positiv bewertet werden
Timo

1
@RichardD Wie würde ich Python-Devel für Python3 mit yum installieren?
Joshua Gomez

Antworten:


163

Das python-devPaket wird für die Kompilierung von Python-Erweiterungen benötigt, die in C oder C ++ geschrieben sind psycopg2. Wenn Sie eine Debian-basierte Distribution (z. B. Ubuntu) ausführen, können Sie diese installieren, python-devindem Sie sie ausführen

sudo apt install python-dev

oder

sudo apt install python3-dev

abhängig von Ihrer Python-Version (zum Beispiel sudo apt install python3.8-dev).

Fahren psycopg2Sie danach virtualenvwie gewohnt mit der Installation in Ihrer Umgebung fort.


13
Wenn Sie Python 3 verwenden, funktioniert dies möglicherweise nicht (oder wird es definitiv nicht). Sie müssen dies apt-get install python3-devstattdessen tun .
Paul Whalen

17
Ich musste apt-get install python3.5-devfür Python 3.5 tun
oder Duan

7
Gleiches gilt für python3.7-dev🤔
Nik

2
das gleiche für python3.8-dev!
22.

2
Ich denke, hier könnte es ein Muster geben ... 🧐
lanzz

38

Zum Ubuntu 14.04 Docker-Bild hat python:3.4.3-slimdiese Kombination bei mir funktioniert:

sudo apt-get update
sudo apt-get install -y build-essential
sudo apt-get install -y python3.4-dev
sudo apt-get install -y libpq-dev

pip3 install psycopg2

build-essentialPaket beachten . In meinem Fall war es entscheidend.


4
Der Build Essential hat es auch für mich getan. Verwenden von Ubuntu 16.04 und Python 2.7.12
wilblack

Hier gilt das gleiche! build-essentialwar das fehlende Stück.
ankush981

Ubuntu 16.04, Python 3.5, das gleiche für mich - Build-Essential fehlte.
Max Malysh

12

In meinem Fall (Ubuntu 12.04 mit Python 2.7 und Python 3.4 installiert)

sudo apt-get install python3-dev 

war nicht genug. Was war der Trick war

sudo apt-get install python3.4-dev

Großartig! Es war auch mein Problem.
Vitalii Ponomar

3

Installieren Sie zunächst python-dev

sudo apt-get install python-dev

Versuchen Sie Folgendes, um die Verwendung zu vereinfachen:

Dies hängt damit zusammen, dass die Codierung Ihres Terminals nicht auf UTF-8 eingestellt ist. Hier ist mein Terminal:

$ echo $LANG
es_UY.UTF-8
$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89'
>>> s1 = s.decode('utf-8')
>>> print s1
(。・ω・。)ノ
>>> 

Wenn ich deaktiviert habe, habe ich den gleichen Fehler erhalten, den Sie erhalten haben:

$ unset LANG
$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89'
>>> s1 = s.decode('utf-8')
>>> print s1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in range(128)
>>> 

1
Der Unicode-Fehler hängt einfach mit der Visualisierung zusammen. Der schwerwiegende Fehler besteht darin, dass die Python.hHeader-Datei nicht gefunden werden kann.
Lanzz

1
Vielen Dank, ich musste auch Server dev installieren: sudo apt-get install postgresql-server-dev-9.1 oder 9.3, wenn postgres 9.3
radtek

3

Debian:

sudo apt-get install gcc

Warum wurde das abgelehnt? Dies löste das Problem für mich. gcc ist in Ubuntu 19 nicht standardmäßig vorinstalliert.
Saturnix

2

Für das neueste Python 3.7 musste ich installieren python3.7-dev:

sudo apt-get install -y python3.7-dev


2

Ich habe die ganze Lösung ausprobiert, aber es funktioniert nur

pip install psycopg2-binary

Nach der Installation von lib können Sie import psycopg2 in Python-Datei und dann weitere DB-Verbindungskonfiguration verwenden


1

Unter Amazon Linux in einem Python3-Venv musste ich Folgendes tun:

sudo yum install python3-devel postgresql-devel
pip install psycopg2

0

sudo apt-get install -y build-essential Nachdem ich alles ohne Erfolg ausprobiert hatte, half mir dies, mein Problem zu lösen.


0

In meinem Fall habe ich versucht, psysopg2 Version 2.7.3.2 in einer virtuellen Umgebung mit Python3.8 zu installieren.

Der obige Fehler ist aufgetreten.

Wie sich herausstellte, benötigt python3.8 psycopg2 Version 2.8 oder höher .

Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.