我有一个Django项目,我想与PostgreSQL数据库一起使用,但我遇到了psycopg2的问题。当我尝试运行开发服务器或执行syncdb时,我收到此错误。我的项目是使用Django 1.6运行Python 3.4的virtualenv。
Traceback (most recent call last):
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 22, in <module>
import psycopg2.extensions
ImportError: No module named 'psycopg2.extensions'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/core/management/base.py", line 280, in execute
translation.activate('en-us')
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 130, in activate
return _trans.activate(language)
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 188, in activate
_active.value = translation(language)
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 177, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 159, in _fetch
app = import_module(appname)
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1448, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/contrib/admin/__init__.py", line 6, in <module>
from django.contrib.admin.sites import AdminSite, site
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 4, in <module>
from django.contrib.admin.forms import AdminAuthenticationForm
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/contrib/admin/forms.py", line 6, in <module>
from django.contrib.auth.forms import AuthenticationForm
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/contrib/auth/forms.py", line 17, in <module>
from django.contrib.auth.models import User
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/contrib/auth/models.py", line 48, in <module>
class Permission(models.Model):
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/db/models/base.py", line 96, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/db/models/base.py", line 264, in add_to_class
value.contribute_to_class(cls, name)
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/db/models/options.py", line 124, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/db/__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/db/utils.py", line 198, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/db/utils.py", line 113, in load_backend
return import_module('%s.base' % backend_name)
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/gabriel/DevSpace/Django_Projects/Kevmo/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 25, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2.extensions'
我所采取的措施因为我没有在virtualenv中安装模块,这是通过pip冻结确认的,所以我尝试使用pip安装它,但安装程序失败并且psycopg2,根据pip freeze仍然没有& #39;已安装。奇怪的是我可以使用python解释器导入psycopg2但是Django由于某种原因无法看到它并且它没有在pip中列出。这是我遇到问题的地方,在运行pip install psycopg2
之后我收到此错误消息。
Creating build/temp.linux-i686-3.4/psycopg
i686-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.5.3 (dt dec pq3 ext)" -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -DPG_VERSION_HEX=0x090304 -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -I/usr/include/python3.4m -I/home/gabriel/DevSpace/Django_Projects/Kevmo/include/python3.4m -I. -I/usr/include/postgresql -I/usr/include/postgresql/9.3/server -c psycopg/psycopgmodule.c -o build/temp.linux-i686-3.4/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 'i686-linux-gnu-gcc' failed with exit status 1
----------------------------------------
Cleaning up...
Command /home/gabriel/DevSpace/Django_Projects/Kevmo/bin/python3.4 -c "import setuptools, tokenize;__file__='/home/gabriel/DevSpace/Django_Projects/Kevmo/build/psycopg2/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-36y2vwa9-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/gabriel/DevSpace/Django_Projects/Kevmo/include/site/python3.4 failed with error code 1 in /home/gabriel/DevSpace/Django_Projects/Kevmo/build/psycopg2
Storing debug log for failure in /tmp/tmpc3blqa5r
我在SO和其他一些地方环顾四周,发现有些人似乎和我有同样的问题,比如这个人,How to install psycopg2 with "pip" on Python?,每个人似乎都说我的问题将会得到解决通过执行包含必要C组件的apt-get install libpq-dev python-dev
来执行安装。不幸的是我已经拥有这些软件包,以及PostgreSQL和postgreSQL开发工具,所以我不知道应该怎么做才能解决这个错误。有没有人知道造成这个错误的另一种方法是不是缺少包裹?
答案 0 :(得分:3)
为了解决这个问题,我安装了Python3-dev,就像Leonardo建议的那样。我试图使用python 2开发工具,这显然不起作用。