繁体   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