簡體   English   中英

無法安裝 psycopg2 Ubuntu

[英]Cannot install psycopg2 Ubuntu

試圖讓服務器為 django 項目做好准備,但我遇到了一些關於 postgres 設置的問題。

我正在關注本指南: https ://jee-appy.blogspot.com/2017/01/deply-django-with-nginx.html

我在第 5 步:

  1. 現在,我們需要配置 postgreSQL 以便它可以與我們的 Django 應用程序通信。 為此,請安裝 psycopg2 數據庫適配器。 但是這個適配器有一些包依賴,所以先安裝它們。

跑:

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

然后...

(django_env) $ pip install psycopg2

我這樣做了,在說明中的這一點,我收到“編譯失敗”錯誤。

我嘗試了這個堆棧交換問題中建議的解決方案: Trouble with psycopg2 in virtualenv python3 for use with Django和這個: Cannot install psycopg2 on virtualenv

我是一個非常喜歡 linux 和 django 的菜鳥,所以如果這是一個重復的問題,請憐憫我並留下評論,解釋為什么你在這樣做時將其標記為重復。

謝謝你的時間!

這是導致它的輸出和安裝命令:

(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('\r\n', '\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('\r\n', '\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

您已經為 Python 3 安裝了 python-dev 庫。您的 psycopg2 安裝使用的是 python 2.7

如果你想使用 Python 3:

確保你已經安裝了 python3-pip

sudo apt-get install python3-pip

然后:

pip3 install psycopg2

如果仍然失敗,請嘗試為 postgresql 安裝開發標頭:

sudo apt install python3-dev libpq-dev
pip3 install psycopg2

如果在 Python 2 上

sudo apt update
sudo apt-get install postgresql postgresql-contrib
sudo apt-get install libpq-dev
sudo apt-get install python-dev
sudo apt-get install python-pip
pip2 install psycopg2-binary

如果在 Ubuntu 20.04 或更高版本上

sudo apt-get install build-essential

然后為 Python 2 或 3 運行其他步驟。

我在一個只安裝了 python2.7.6 的 ubuntu docker 鏡像上測試我的應用程序(就像在大多數大型組織中發生的那樣)

如果您使用的是 python2.x(盡管您應該計划盡快遷移到 3.x),請檢查以下依賴項:

sudo apt update
sudo apt-get install postgresql postgresql-contrib
sudo apt-get install libpq-dev # this is required as psycopg2 uses pg_config
sudo apt-get install python-dev
sudo apt-get install python-pip

現在使用以下命令安裝 psycopg2:

pip2 install psycopg2-binary

我嘗試在virtualenv中安裝它時遇到了同樣的錯誤(使用 python3

我通過安裝以前版本的psycopg2解決了這個問題。

pip install psycopg2==2.7.5

請注意,在許多發行版中,默認情況下未安裝針對庫進行編譯所需的開發頭文件。 對於 Ubuntu 上的 psycopg2,您需要 python3 和 postgresql 標頭:

sudo apt install python3-dev libpq-dev
pip3 install psycopg2

這些可以安裝在您激活的虛擬環境中。

這解決了我的問題。 我正在使用 Python 3.8.2、Ubuntu 20.04 LTS:

sudo apt-get install python3-dev
sudo apt-get install python3-pip
pip install psycopg2

要在 ubuntu 或 mate 20 中安裝psycopg2 ,您首先需要安裝:

sudo apt install libpq-dev

接着:

pip3 install psycopg2

就我而言,當我運行pip install -r requirements.txt在 Ubuntu 機器上安裝 Django 項目的所有軟件包時遇到了這個問題,我遇到了這個錯誤和許多其他安裝錯誤。

為了解決這個問題,我運行了以下命令:

sudo apt install postgresql postgresql-contrib  

sudo apt install libpq-dev

sudo apt install python3-dev

sudo apt install python3-pip

sudo apt install python3-psycopg2

pip3 install psycopg2

pip3 install psycopg2-binary

另外,還要檢查 Ubuntu 和 Python 以及 Psycopg 版本是否兼容。

另外,@Arghya Bhattacharya 回答pip install aiopg ,當我第二次遇到它時解決了這個問題。

我也必須在我的 Ubuntu 20.04 LTS 上安裝這個:

sudo apt-get install build-essential

我在我的 ubuntu 18.04 LTS 操作系統上遇到了同樣的問題。 此外,在Pillow中面臨一些問題。 在這兩種情況下( psycopg2Pillow ),這些命令都解決了我的問題。

sudo apt install -y build-essential libssl-dev libffi-dev python3-dev libjpeg-dev libjpeg8-dev

sudo apt install libpq-dev

sudo apt-get install python3-pip

注意:我已經在python=3.8.5環境中安裝了psycopg2

確保您為 python 版本使用正確的 psycopg 版本。

python 3.8 的示例。

python 3.8, the supported version is psycopg 2.8.4.

參考ubuntu 20.04 + python 3.8,pip install psycopg2==2.7.3.2 錯誤#1106

如果您在 Ubuntu 上安裝了多個 python 版本:根據您的 pipenv 文件所需的 python 版本,您需要安裝相應的 dev 文件。

我收到了這個錯誤,我的默認 python 版本是 3.8,但是 pipenv 文件需要 Python3.9 版本。 所以我安裝了python3.9 dev。

$sudo apt install python3.9-dev

我使用另一個本身在內部安裝 psycopg2 的包解決了這個問題。

pip install aiopg

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM