试图让服务器为django项目做好准备,我遇到了一些设置postgres的问题。
我正在遵循这个指南:https://jee-appy.blogspot.com/2017/01/deply-django-with-nginx.html
我在第5步:
- 现在,我们需要配置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 noob,所以如果这是一个重复的问题,请怜悯我并发表评论,解释为什么你将它标记为重复,因为你这样做。
谢谢你的时间!
这是导致它的输出和安装命令:
(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
确保你已经安装了python3-pip
sudo apt-get install python3-pip
然后:
pip3 install psycopg2
我在一个只安装了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