gpt4 book ai didi

python - Cookiecutter Django 与 PostgreSQL 不兼容

转载 作者:行者123 更新时间:2023-11-29 13:55:35 25 4
gpt4 key购买 nike

我一直在尝试使用 Cookiecutter Django 启动一个项目,但无法正常连接数据库。

这是我所做的:

pip install cookiecutter

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

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

然后,我运行 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 用户发了电子邮件,他建议我跳过所有关于权限等的东西,“只使用其中的 createdb 部分”,这就是我上面解释的。我仍然遇到同样的错误。

我的一个理论是它与我机器上的多个 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'

这也让我感到困惑,因为我显然已经按照上面的 pip 调用安装了 Django。

最佳答案

您的主要问题是 psycopg2 未加载。修复:

  1. 确保您的 virtualenv 处于事件状态。
  2. 从您的虚拟环境中卸载 psycopg2(pip uninstall psycopg2)。检查您的 virtualenv 的站点包目录以确保它已经消失。
  3. 将 psycopg2 重新安装到您的虚拟环境中。由于看起来您使用的是 OS X,因此您应该能够通过 pip install psycopg2 或通过重新安装本地要求来执行此操作。

然后要创建您的数据库,请执行 createdb shred 或您的数据库的任何名称,如您的设置中指定的那样。这应该与您为 repo_name 输入的值相匹配。

关于python - Cookiecutter Django 与 PostgreSQL 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32834839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com