gpt4 book ai didi

django - Django 应用程序的 Heroku 推送获取 "No module named psycopg2.extensions"

转载 作者:行者123 更新时间:2023-11-29 11:23:07 24 4
gpt4 key购买 nike

我正在尝试将 django 应用程序推送到 heroku,但出现了一个我在 heroku 或 stackoverflow 论坛中从未见过的错误。我正在使用 postgres。

我不确定从哪里开始;我在任何文档中都看不到有什么问题。主要错误是:

导入错误:没有名为 psycopg2.extensions 的模块

...但是 heroku 的配置是将其设置为引擎的配置,我不知道该怎么做。

我与 heroku 的对话,以确保它了解 postgres

$ heroku addons:add heroku-postgresql:dev
Adding heroku-postgresql:dev on morning-crag-1585... done, v24 (free)
Attached as HEROKU_POSTGRESQL_OLIVE_URL
Database has been created and is available
! This database is empty. If upgrading, you can transfer
! data from another database with pgbackups:restore.
.
Use `heroku addons:docs heroku-postgresql:dev` to view documentation.
$ heroku pg:wait
$ heroku config | grep HEROKU_POSTGRESQL
HEROKU_POSTGRESQL_OLIVE_URL: postgres://<blah-blah-blah>@ec2-<ip-address>.compute-1.amazonaws.com:5432/<blah-blah>
$ heroku pg:promote HEROKU_POSTGRESQL_OLIVE_URL
Promoting HEROKU_POSTGRESQL_OLIVE_URL to DATABASE_URL... done

我的要求.txt:

Django==1.4.2
wsgiref==0.1.2
dj-database-url==0.2.1

我的 settings.py 中的一个片段:

print "Got here before!\n"
try:
import dj_database_url
DATABASES = { 'default': dj_database_url.config(default='postgres://localhost') }
except:
print "Unexpected error:", sys.exc_info()

print DATABASES

当我推送到 heroku 时,我从 heroku logs 得到这个:

heroku[slugc]: Slug compilation finished
heroku[web.1]: Starting process with command `python ./manage.py runserver 0.0.0.0:30550 --noreload`
app[web.1]: Got here before!
app[web.1]:
app[web.1]: {'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': '<blah blah>', 'HOST': 'ec2-<iip-address>.compute-1.amazonaws.com', 'USER': '<blah blah>', 'PASSWORD': '<blah>', 'PORT': 5432}}
app[web.1]:
app[web.1]: Validating models...
app[web.1]: Traceback (most recent call last):
app[web.1]: File "./manage.py", line 10, in <module>
app[web.1]: execute_from_command_line(sys.argv)
app[web.1]: utility.execute()
app[web.1]: self.execute(*args, **options.__dict__)
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
app[web.1]: self.fetch_command(subcommand).run_from_argv(self.argv)
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
app[web.1]: self.inner_run(*args, **options)
app[web.1]: self.validate(display_num_errors=True)
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
app[web.1]: num_errors = get_validation_errors(s, app)
app[web.1]: self.run(*args, **options)
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/utils.py", line 92, in __getitem__
app[web.1]: output = self.handle(*args, **options)
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/validation.py", line 23, in get_validation_errors
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
app[web.1]: from django.db import models, connection
app[web.1]: return import_module('.base', backend_name)
app[web.1]: __import__(name)
app[web.1]: ImportError: No module named psycopg2.extensions
app[web.1]: return getattr(connections[DEFAULT_DB_ALIAS], item)
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/creation.py", line 1, in <module>
app[web.1]: backend = load_backend(db['ENGINE'])
app[web.1]: import psycopg2.extensions
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/__init__.py", line 40, in <module>
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 69, in handle
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 80, in run
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/utils.py", line 24, in load_backend
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
app[web.1]: backend = load_backend(connection.settings_dict['ENGINE'])
app[web.1]: from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation
app[web.1]: File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 13, in <module>
heroku[web.1]: Process exited with status 1
heroku[web.1]: State changed from starting to crashed

最佳答案

您需要将 psycopg2 安装添加到您的 requirements.txt 文件中。它是数据库适配器 - python 与 PostgreSQL 的接口(interface)方式,您确实不需要安装,因为 heroku 需要安装。

只需将 psycopg2==2.4.4 行添加到您的 requirements.txt 文件中。

顺便说一下,数据库附加组件本身并不是必需的。 dj-database-url 会自动处理它。查看heroku docs获取分步指南。

关于django - Django 应用程序的 Heroku 推送获取 "No module named psycopg2.extensions",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13620051/

24 4 0