简体   繁体   English

无法安装 psycopg2 Ubuntu

[英]Cannot install psycopg2 Ubuntu

Trying to get a server ready for a django project and I'm running into some issues with setup for postgres.试图让服务器为 django 项目做好准备,但我遇到了一些关于 postgres 设置的问题。

I'm following this guide: https://jee-appy.blogspot.com/2017/01/deply-django-with-nginx.html我正在关注本指南: https ://jee-appy.blogspot.com/2017/01/deply-django-with-nginx.html

And I'm at step 5:我在第 5 步:

  1. Now, we need to configure postgreSQL so that it can communicate with our Django application.现在,我们需要配置 postgreSQL 以便它可以与我们的 Django 应用程序通信。 For this, install psycopg2 database adapter.为此,请安装 psycopg2 数据库适配器。 But this adapter have some package dependencies, so first install them.但是这个适配器有一些包依赖,所以先安装它们。

run:跑:

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

then...然后...

(django_env) $ pip install psycopg2 (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我尝试了这个堆栈交换问题中建议的解决方案: Trouble with psycopg2 in virtualenv python3 for use with Django和这个: 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.我是一个非常喜欢 linux 和 django 的菜鸟,所以如果这是一个重复的问题,请怜悯我并留下评论,解释为什么你在这样做时将其标记为重复。

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('\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

You've installed the python-dev libraries for Python 3. Your psycopg2 install is using python 2.7您已经为 Python 3 安装了 python-dev 库。您的 psycopg2 安装使用的是 python 2.7

If you want to use Python 3:如果你想使用 Python 3:

Make sure you've installed python3-pip确保你已经安装了 python3-pip

sudo apt-get install python3-pip

Then:然后:

pip3 install psycopg2

If that still fails, try installing the development headers for postgresql:如果仍然失败,请尝试为 postgresql 安装开发标头:

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

If on Python 2如果在 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

If on Ubuntu 20.04 or above如果在 Ubuntu 20.04 或更高版本上

sudo apt-get install build-essential

Then run the other steps for either Python 2 or 3.然后为 Python 2 或 3 运行其他步骤。

I was testing my application on a ubuntu docker image that only has python2.7.6 installed (as happens in most big orgs)我在一个只安装了 python2.7.6 的 ubuntu docker 镜像上测试我的应用程序(就像在大多数大型组织中发生的那样)

If you are using python2.x (though you should plan to move to 3.x asap), check the below dependencies:如果您使用的是 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

Now install psycopg2 using:现在使用以下命令安装 psycopg2:

pip2 install psycopg2-binary

I had the same error trying to install it in a virtualenv ( with python3 )我尝试在virtualenv中安装它时遇到了同样的错误(使用 python3

I solved it by installing a previous version of psycopg2 .我通过安装以前版本的psycopg2解决了这个问题。

pip install psycopg2==2.7.5 pip install psycopg2==2.7.5

Note that on many distros, the development headers needed for compiling against libraries are not installed by default.请注意,在许多发行版中,默认情况下未安装针对库进行编译所需的开发头文件。 For psycopg2 on Ubuntu you'll need the python3 and postgresql headers:对于 Ubuntu 上的 psycopg2,您需要 python3 和 postgresql 标头:

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

These can be installed in your activated virtual environment.这些可以安装在您激活的虚拟环境中。

This solved mine.这解决了我的问题。 I am using Python 3.8.2, Ubuntu 20.04 LTS:我正在使用 Python 3.8.2、Ubuntu 20.04 LTS:

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

To install psycopg2 in ubuntu or mate 20 you need first to install:要在 ubuntu 或 mate 20 中安装psycopg2 ,您首先需要安装:

sudo apt install libpq-dev

and then:接着:

pip3 install psycopg2

In my case, I was facing this problem when I ran pip install -r requirements.txt to install all packages for a Django project on an Ubuntu machine, I ran into this error and many other installation errors.就我而言,当我运行pip install -r requirements.txt在 Ubuntu 机器上安装 Django 项目的所有软件包时遇到了这个问题,我遇到了这个错误和许多其他安装错误。

To solve this one, I ran the following commands:为了解决这个问题,我运行了以下命令:

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

Plus, also check if the Ubuntu and Python and Psycopg versions are compatible together.另外,还要检查 Ubuntu 和 Python 以及 Psycopg 版本是否兼容。

Also, @Arghya Bhattacharya answers pip install aiopg , solve the issue when i ran into it the second time.另外,@Arghya Bhattacharya 回答pip install aiopg ,当我第二次遇到它时解决了这个问题。

我也必须在我的 Ubuntu 20.04 LTS 上安装这个:

sudo apt-get install build-essential

I faced same issue on my ubuntu 18.04 LTS OS.我在我的 ubuntu 18.04 LTS 操作系统上遇到了同样的问题。 Also, face some issues in Pillow .此外,在Pillow中面临一些问题。 In both cases ( psycopg2 and Pillow ) these command solved my issue.在这两种情况下( 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

Note: I have installed psycopg2 in python=3.8.5 environment.注意:我已经在python=3.8.5环境中安装了psycopg2

make sure you are using the correct psycopg version for the python version.确保您为 python 版本使用正确的 psycopg 版本。

Example for python 3.8. python 3.8 的示例。

python 3.8, the supported version is psycopg 2.8.4.

Reference ubuntu 20.04 + python 3.8 , pip install psycopg2==2.7.3.2 error #1106参考ubuntu 20.04 + python 3.8,pip install psycopg2==2.7.3.2 错误#1106

In case you have several python versions installed on your Ubuntu: Based on the python version your your pipenv file requires, you need to install the corresponding dev file.如果您在 Ubuntu 上安装了多个 python 版本:根据您的 pipenv 文件所需的 python 版本,您需要安装相应的 dev 文件。

I was getting this error and my default python version was 3.8 but the pipenv file was requiring the Python3.9 version.我收到了这个错误,我的默认 python 版本是 3.8,但是 pipenv 文件需要 Python3.9 版本。 So I installed the python3.9 dev.所以我安装了python3.9 dev。

$sudo apt install 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