Cookiecutter Django对PostgreSQL并不好玩

时间:2015-09-29 02:41:57

标签: python django postgresql python-2.7 python-3.x

我一直试图让一个项目开始使用Cookiecutter Django,并且无法正常进行数据库连接。

这就是我所做的:

pip install cookiecutter

cookiecutter https://github.com/pydanny/cookiecutter-django.git

然后填写所有问题的答案并按详细here运行pip install -r requirements/local.txt

然后,我运行psql并执行:

CREATE DATABASE example;
CREATE USER example_user WITH PASSWORD 'password';

其次是export DATABASE_URL=postgres://example_user:password@localhost:5432/example(在psql之外,但在我的virtualenv中,虽然我也在virtualenv之外尝试过)。

然后,在上面的链接中详细说明运行python manage.py migrate后,我明白了:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/django/contrib/auth/models.py", line 41, in <module>
    class Permission(models.Model):
  File "/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/django/db/models/base.py", line 139, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/django/db/models/base.py", line 324, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/django/db/models/options.py", line 250, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/django/db/utils.py", line 240, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 24, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _lo_lseek64
  Referenced from: /Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/psycopg2/_psycopg.so
  Expected in: /usr/lib/libpq.5.dylib in /Users/maxwellskala/.virtualenvs/shred/lib/python2.7/site-packages/psycopg2/_psycopg.so

为了完整披露,我首先尝试按照here指南以及官方文档进行操作,但没有成功。

在一堆不成功的谷歌搜索之后,我发生了故障并通过电子邮件发送了另一个SO用户,他建议我跳过关于权限的所有内容,以及&#34;只需使用其创建的部分&#34;这是我上面解释的。我仍然得到同样的错误。

我的一个理论是它与我机器上的多个Python版本有关。请参阅this问题。基本上,如果(在我正确的virtualenv中)我运行python manage.py migrate我得到上述错误,但如果我运行python 3.4 manage.py migrate,我得到

Traceback (most recent call last): File "manage.py", line 8, in <module> from django.core.management import execute_from_command_line ImportError: No module named 'django'

这也让我感到困惑,因为我根据上面的点子通知显然安装了Django。

1 个答案:

答案 0 :(得分:0)

你的主要问题是psycopg2没有加载。修复:

  1. 确保您的virtualenv处于活动状态。
  2. 从virtualenv(pip uninstall psycopg2)卸载psycopg2。检查你的virtualenv的网站包dir以确保它已经消失。
  3. 将psycopg2重新安装到virtualenv中。由于您看起来像是在OS X上,因此您应该可以通过pip install psycopg2或重新安装本地要求来执行此操作。
  4. 然后,要创建数据库,请按照设置中的指定执行createdb shred或数据库名称。这应该与您为repo_name输入的值相匹配。