我正在尝试将 Django 站点部署到 heroku 并收到以下错误片段:
2023-05-05T08:42:38.330672+00:00 app[web.1]: from psycopg2._psycopg import ( # noqa
2023-05-05T08:42:38.330672+00:00 app[web.1]: SystemError: initialization of _psycopg raised unreported exception
2023-05-05T08:42:38.330718+00:00 app[web.1]: [2023-05-05 08:42:38 +0000] [8] [INFO] Worker exiting (pid: 8)
2023-05-05T08:42:38.367087+00:00 app[web.1]: [2023-05-05 08:42:38 +0000] [2] [WARNING] Worker with pid 8 was terminated due to signal 15
2023-05-05T08:42:38.465035+00:00 app[web.1]: [2023-05-05 08:42:38 +0000] [2] [INFO] Shutting down: Master
2023-05-05T08:42:38.465071+00:00 app[web.1]: [2023-05-05 08:42:38 +0000] [2] [INFO] Reason: Worker failed to boot.
2023-05-05T08:42:38.616316+00:00 heroku[web.1]: Process exited with status 3
2023-05-05T08:42:38.666808+00:00 heroku[web.1]: State changed from starting to crashed
2023-05-05T08:42:39.000000+00:00 app[api]: Build succeeded
2023-05-05T08:42:44.863150+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=a-coders-journey.herokuapp.com request_id=d8e2f6b2-c4f5-461f-8914-a8e84dc13551 fwd="93.204.33.78" dyno= connect= service= status=503 bytes= protocol=https
我已确保我的requirements.txt 文件的版本具有二进制版本(psycopg2-binary)以及所有适用的要求:
#
# This file is autogenerated by pip-compile with Python 3.8
# by the following command:
#
# pip-compile --output-file=requirements.txt requirements.in
#
asgiref==3.3.4
# via
# -r requirements.in
# django
certifi==2022.12.7
# via
# cloudinary
# requests
cffi==1.15.1
# via cryptography
charset-normalizer==3.1.0
# via requests
cloudinary==1.25.0
# via
# -r requirements.in
# dj3-cloudinary-storage
cryptography==3.4.8
# via
# -r requirements.in
# pyjwt
defusedxml==0.7.1
# via python3-openid
dj-database-url==0.5.0
# via -r requirements.in
dj3-cloudinary-storage==0.0.5
# via -r requirements.in
django==3.2.3
# via
# -r requirements.in
# django-allauth
# django-summernote
django-allauth==0.44.0
# via -r requirements.in
django-crispy-forms==1.11.2
# via -r requirements.in
django-summernote==0.8.11.6
# via -r requirements.in
gunicorn==20.1.0
# via -r requirements.in
idna==3.4
# via requests
oauthlib==3.1.1
# via
# -r requirements.in
# requests-oauthlib
pillow==9.5.0
# via -r requirements.in
psycopg2-binary
# via -r requirements.in
pycparser==2.21
# via cffi
pyjwt[crypto]==2.1.0
# via
# -r requirements.in
# django-allauth
python3-openid==3.2.0
# via
# -r requirements.in
# django-allauth
pytz==2021.1
# via
# -r requirements.in
# django
requests==2.30.0
# via
# dj3-cloudinary-storage
# django-allauth
# requests-oauthlib
requests-oauthlib==1.3.0
# via
# -r requirements.in
# django-allauth
six==1.16.0
# via cloudinary
sqlparse==0.4.1
# via
# -r requirements.in
# django
urllib3==2.0.2
# via
# cloudinary
# requests
# The following packages are considered to be unsafe in a requirements file:
# setuptools
已安装。我用heroku开了一张支持票,他们给了我链接:https://devcenter.heroku.com/articles/connecting-heroku-postgres#connecting-in-python,我遵循了,但没有成功。我的 settings.py 文件,与导入相关:
from pathlib import Path
import os
import psycopg2
import dj_database_url
if os.path.isfile('env.py'):
import env
#,,,other code then:
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
then at the very bottom of the settings.py:
DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)
我在 Django 中的 Python 版本:Python 3.8.12
Heroku Python 版本:Python 3.11.3
我做错了什么?为什么 psycopg2 在初始化时出错?预先感谢。
我从旧版本的 psycopg2 迁移到二进制版本。我尝试了 Heroku 支持文档演练,
我尝试安装一个runtime.txt文件并强制heroku应用程序使用与我的Django版本相同的版本,并且我已经确保我的env.py文件的内容正确指向我正在使用的elephantSQL数据库,两者这些变量也在我的 Heroku 配置变量中。
忽略连接到Python部分,它与你无关,对于Django应用程序,你只需要“连接Django”部分。
Django部分应该如下
# Define database URL from the environment file
DATABASE_URL = os.environ['DATABASE_URL']
# Configure default database
DATABASES = {
'default': dj_database_url.config(
conn_max_age=600,
conn_health_checks=True,
ssl_require=True,
),
}
如果您之前解决过这个问题,有人可以帮我吗?当我在requirements.txt 文件中添加以下包(作为现有包的补充)时,我曾经能够避免此错误:
existing one:psycopg2==2.8.6
additional one added:psycopg2-binary>=2.8.6
在重新部署期间,我再次遇到相同的错误。这次我只更改了 virtualenv 和应用程序文件夹位置。