2014-06-11 56 views
4

我有一個我想用於PostgreSQL數據庫的Django項目,但是我遇到了psycopg2問題。當我嘗試運行dev服務器或執行syncdb時,出現此錯誤。我的項目位於使用Django 1.6運行Python 3.4的virtualenv中。用於Django的virtualenv python3中的psycopg2問題

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安裝,但安裝失敗,psycopg2,根據點子凍結仍然沒有安裝。奇怪的是,我可以使用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

嘗試'apt-get build-dep psycopg2' –

+0

'apt-get install python-dev'用於Python2,嘗試安裝'Python3-dev' –

+0

python3-dev做到了。謝謝!你有什麼想法,爲什麼我能夠導入psycopg2沒有安裝它? – user3282276

回答

3

要解決此問題,我按照萊昂納多的建議安裝了Python3-dev。我試圖使用顯然不工作的python 2開發工具。