简体   繁体   English

尝试为 python (psycopg2) 安装 Postgres 时出错

[英]Error trying to install Postgres for python (psycopg2)

I tried to install psycopg2 to my environment, but I get the following error:我尝试将 psycopg2 安装到我的环境中,但出现以下错误:

(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)

I also get errors trying to install gunicorn.我在尝试安装 gunicorn 时也遇到了错误。 I followed this example here but nothing.我在这里遵循了这个例子,但什么也没有。 My virtual environment was created using the --no-site-packages flag.我的虚拟环境是使用--no-site-packages标志创建的。 I use ubuntu 13.10 and greek environment.我使用 ubuntu 13.10 和希腊环境。 Maybe it's that?也许就是这样?

The python-dev package is required for compilation of Python extensions written in C or C++, like psycopg2 .编译用 C 或 C++ 编写的 Python 扩展需要python-dev包,如psycopg2 If you're running a Debian-based distribution (eg Ubuntu), you can install python-dev by running如果您运行的是基于 Debian 的发行版(例如 Ubuntu),您可以通过运行安装python-dev

sudo apt install python-dev

or或者

sudo apt install python3-dev

depending on your python version (for example, sudo apt install python3.8-dev ).取决于您的 python 版本(例如, sudo apt install python3.8-dev )。

After that, proceed to install psycopg2 in your virtualenv environment as usual.之后,继续像往常一样在您的virtualenv环境中安装psycopg2

For Ubuntu 14.04 , from Docker image python:3.4.3-slim this combination worked for me:对于Ubuntu 14.04 ,来自 Docker 镜像python:3.4.3-slim这个组合对我有用:

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

Note build-essential package.注意build-essential包。 It was crucial in my case.这对我来说至关重要。

In my case (ubuntu 12.04 with python 2.7 and python 3.4 installed)在我的情况下(安装了 python 2.7 和 python 3.4 的 ubuntu 12.04)

sudo apt-get install python3-dev 

was not enough.还不够。 What did the trick was诀窍是什么

sudo apt-get install python3.4-dev

I tried all the solution but only works be我尝试了所有的解决方案,但只适用于

pip install psycopg2-binary

after installing lib you can use import psycopg2 in python file and then further db connection config安装lib后,您可以在python文件中使用import psycopg2 ,然后进一步使用db连接配置

On Amazon Linux within a python3 venv I had to do the following:python3 venv 中的Amazon Linux 上,我必须执行以下操作:

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

First of all install python-dev首先安装python-dev

sudo apt-get install python-dev

And to adecuate use try this:并适当使用试试这个:

This is to do with the encoding of your terminal not being set to UTF-8.这与您的终端的编码未设置为 UTF-8 有关。 Here is my 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
(。・ω・。)ノ
>>> 

If I unset I got the same error you got:如果我取消设置,我会遇到与您相同的错误:

$ 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)
>>> 

Debian:

sudo apt-get install gcc

对于最新的 Python 3.7,我必须安装python3.7-dev

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

sudo apt-get install -y build-essential 在尝试了一切但没有成功后,这帮助我解决了我的问题。

In my case, I tried to install psysopg2 version 2.7.3.2 in a virtual environment with python3.8.就我而言,我尝试使用 python3.8 在虚拟环境中安装 psysopg2 版本 2.7.3.2。

The above error occurred.出现上述错误。

As it turned out, python3.8 requires psycopg2 version 2.8 or higher .事实证明, python3.8 需要 psycopg2 版本 2.8 或更高版本

Remove the current virtual environment and create another one.删除当前的虚拟环境并创建另一个。 Install the dependencies first then the rest you've been using.首先安装依赖项,然后安装您一直在使用的其他依赖项。 This works这有效

I'm using the Ubuntu distribution of Linux and did the following:我正在使用 Linux 的 Ubuntu 分布并执行了以下操作:

Install the latest version of the python3-dev library so C extensions work for python. The first command updates the package list in Ubuntu so the latest version of python3-dev is installed.安装最新版本的 python3-dev 库,以便 C 扩展适用于 python。第一个命令更新 Ubuntu 中的 package 列表,以便安装最新版本的 python3-dev。

sudo apt-get update sudo apt-get 更新

sudo apt-get install python3-dev sudo apt-get 安装 python3-dev

Then I had to install the the missing development library for PostgreSQL: the "libpq-dev" package.然后我必须为 PostgreSQL 安装缺少的开发库:“libpq-dev”package。

sudo apt-get install libpq-dev sudo apt-get 安装 libpq-dev

After that, I was able to finish installing my dependencies.在那之后,我能够完成我的依赖项的安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM