1

我是一个总编程n00b,刚刚开始学习django。我已经能够使用 SQLite 完成工作,但我对 PostgreSQL 完全停滞不前……我已经浏览了 Stackoverflow 和其他地方几个小时,但一直找不到好的答案。

我的 settings.py 文件:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydb',
        'USER': 'user',
        'PASSWORD': 'passwd',
        'HOST': 'localhost',
        'PORT': '',
}

}

在终端中运行命令:

$ python manage.py validate    

终端输出:

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/validate.py", line 9, in handle_noargs
self.validate(display_num_errors=True)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/validation.py", line 23, in get_validation_errors
from django.db import models, connection
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 92, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 24, in load_backend
return import_module('.base', backend_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 13, in <module>
from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/creation.py", line 1, in <module>
import psycopg2.extensions
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/__init__.py", line 67, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _PQbackendPID
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/_psycopg.so
Expected in: dynamic lookup
4

2 回答 2

1

默认情况下,psycopg2 不会在 OSX 上正确编译。

维护人员建议您尽可能使用 Fink 或 Macports 的版本(http://initd.org/psycopg/install/)。

如果您想/需要从源代码安装,他们提供这些链接来解决常见问题:http: //initd.org/psycopg/articles/2010/11/11/links-about-building-psycopg-mac-os -X/

IIRC,处理此问题的最佳方法是:

  • 设置环境变量以确保它们在您执行 pip/easy_install 时选择正确的 pg_config 二进制文件(它包含您正在构建的 postgresql 客户端/服务器的所有配置信息),或者如果您手动在 setup.cfg 中手动记录它建造。

  • 设置环境变量以明确为您的 osx 安装设置正确的 ARCHFLAGS。有时 python 会尝试在 32 位安装上构建 64 位,反之亦然。

  • 新安装让人们遇到了 libssl 的问题,我从来没有遇到过这些

于 2013-03-10T21:12:53.337 回答
0

Reading the error, I dont think this is a case of psycopg2 not being installed but due to it not loading correctly. Something is wrong here.

I would make sure you have libpq installed (postgresql client libraries), plus the relevant header files. then I would remove psycopg2 and try to install again.

If that fails, make sure the header files are for the same version of libpq as you have installed. It sounds like you are having issues with expected symbol exports not being there, which suggests possible library versioning issues.

于 2012-09-22T02:02:59.110 回答