首页 > 解决方案 > 我的 heroku 应用程序无法导入 Phonenumber_field 模块

问题描述

我正在尝试将 Django 应用程序部署到 Heroku。

这是我的 settings.py 文件:


import os
import dj_database_url
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'aijsndkajskjndkjasnkansndas'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


AUTH_USER_MODEL = 'appname.User'

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'appname.apps.AppnameConfig',
]

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'projectname.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'projectname.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/


STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

这是我的 requirements.txt 文件

django==3.0.8
gunicorn==20.0.4
whitenoise==5.2.0
dj_database_url==0.5.0
django-phonenumber-field==5.0.0
jsonfield==3.1.0

当我尝试使用 git push heroku main 将我的应用程序部署到 heroku 时,我收到一条成功部署的消息。这就是我执行 git push heroku main 时发生的情况:

Users@Users-MacBook-Air projectname % git push heroku master                                               
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 642 bytes | 642.00 KiB/s, done.
Total 6 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Python app detected
remote:  !     Python has released a security update! Please consider upgrading to python-3.8.6
remote:        Learn More: https://devcenter.heroku.com/articles/python-runtimes
remote: -----> Requirements file has been changed, clearing cached dependencies
remote: -----> Installing python-3.8.3
remote: -----> Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip
remote:        Collecting django==3.0.8
remote:          Downloading Django-3.0.8-py3-none-any.whl (7.5 MB)
remote:        Collecting gunicorn==20.0.4
remote:          Downloading gunicorn-20.0.4-py2.py3-none-any.whl (77 kB)
remote:        Collecting whitenoise==5.2.0
remote:          Downloading whitenoise-5.2.0-py2.py3-none-any.whl (19 kB)
remote:        Collecting dj_database_url==0.5.0
remote:          Downloading dj_database_url-0.5.0-py2.py3-none-any.whl (5.5 kB)
remote:        Collecting django-phonenumber-field==5.0.0
remote:          Downloading django_phonenumber_field-5.0.0-py3-none-any.whl (53 kB)
remote:        Collecting jsonfield==3.1.0
remote:          Downloading jsonfield-3.1.0-py3-none-any.whl (8.0 kB)
remote:        Collecting asgiref~=3.2
remote:          Downloading asgiref-3.3.0-py3-none-any.whl (19 kB)
remote:        Collecting sqlparse>=0.2.2
remote:          Downloading sqlparse-0.4.1-py3-none-any.whl (42 kB)
remote:        Collecting pytz
remote:          Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
remote:        Installing collected packages: asgiref, sqlparse, pytz, django, gunicorn, whitenoise, dj-database-url, django-phonenumber-field, jsonfield
remote:        Successfully installed asgiref-3.3.0 dj-database-url-0.5.0 django-3.0.8 django-phonenumber-field-5.0.0 gunicorn-20.0.4 jsonfield-3.1.0 pytz-2020.1 sqlparse-0.4.1 whitenoise-5.2.0
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 57.9M
remote: -----> Launching...
remote:        Released v10
remote:        https://url.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/url.git
   e155d34..115a7bf  master -> master

这是我通过获得的heroku日志heroku logs --tail

2020-10-13T07:27:11.230997+00:00 heroku[web.1]: State changed from crashed to starting
2020-10-13T07:27:17.423587+00:00 heroku[web.1]: Starting process with command `gunicorn projectname.wsgi --log-file -`
2020-10-13T07:27:20.000000+00:00 app[api]: Build succeeded
2020-10-13T07:27:20.987527+00:00 app[web.1]: [2020-10-13 07:27:20 +0000] [4] [INFO] Starting gunicorn 20.0.4
2020-10-13T07:27:20.992427+00:00 app[web.1]: [2020-10-13 07:27:20 +0000] [4] [INFO] Listening at: http://0.0.0.0:18503 (4)
2020-10-13T07:27:20.992713+00:00 app[web.1]: [2020-10-13 07:27:20 +0000] [4] [INFO] Using worker: sync
2020-10-13T07:27:21.001707+00:00 app[web.1]: [2020-10-13 07:27:21 +0000] [9] [INFO] Booting worker with pid: 9
2020-10-13T07:27:21.047773+00:00 app[web.1]: [2020-10-13 07:27:21 +0000] [10] [INFO] Booting worker with pid: 10
2020-10-13T07:27:21.718350+00:00 app[web.1]: [2020-10-13 07:27:21 +0000] [10] [ERROR] Exception in worker process
2020-10-13T07:27:21.718384+00:00 app[web.1]: Traceback (most recent call last):
2020-10-13T07:27:21.718385+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-10-13T07:27:21.718400+00:00 app[web.1]: worker.init_process()
2020-10-13T07:27:21.718401+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 119, in init_process
2020-10-13T07:27:21.718401+00:00 app[web.1]: self.load_wsgi()
2020-10-13T07:27:21.718401+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2020-10-13T07:27:21.718402+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-10-13T07:27:21.718403+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-10-13T07:27:21.718403+00:00 app[web.1]: self.callable = self.load()
2020-10-13T07:27:21.718404+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-10-13T07:27:21.718404+00:00 app[web.1]: return self.load_wsgiapp()
2020-10-13T07:27:21.718404+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-10-13T07:27:21.718405+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-10-13T07:27:21.718405+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 358, in import_app
2020-10-13T07:27:21.718406+00:00 app[web.1]: mod = importlib.import_module(module)
2020-10-13T07:27:21.718406+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2020-10-13T07:27:21.718407+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-10-13T07:27:21.718407+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2020-10-13T07:27:21.718408+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2020-10-13T07:27:21.718408+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2020-10-13T07:27:21.718409+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2020-10-13T07:27:21.718409+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 783, in exec_module
2020-10-13T07:27:21.718409+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-10-13T07:27:21.718410+00:00 app[web.1]: File "/app/projectname/wsgi.py", line 16, in <module>
2020-10-13T07:27:21.718410+00:00 app[web.1]: application = get_wsgi_application()
2020-10-13T07:27:21.718411+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2020-10-13T07:27:21.718411+00:00 app[web.1]: django.setup(set_prefix=False)
2020-10-13T07:27:21.718411+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
2020-10-13T07:27:21.718412+00:00 app[web.1]: apps.populate(settings.INSTALLED_APPS)
2020-10-13T07:27:21.718412+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
2020-10-13T07:27:21.718412+00:00 app[web.1]: app_config.import_models()
2020-10-13T07:27:21.718413+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
2020-10-13T07:27:21.718413+00:00 app[web.1]: self.models_module = import_module(models_module_name)
2020-10-13T07:27:21.718413+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2020-10-13T07:27:21.718414+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-10-13T07:27:21.718414+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2020-10-13T07:27:21.718414+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2020-10-13T07:27:21.718415+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2020-10-13T07:27:21.718415+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2020-10-13T07:27:21.718415+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 783, in exec_module
2020-10-13T07:27:21.718416+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-10-13T07:27:21.718416+00:00 app[web.1]: File "/app/appname/models.py", line 83, in <module>
2020-10-13T07:27:21.718416+00:00 app[web.1]: from phonenumber_field.modelfields import PhoneNumberField
2020-10-13T07:27:21.718417+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/phonenumber_field/modelfields.py", line 7, in <module>
2020-10-13T07:27:21.718417+00:00 app[web.1]: from phonenumber_field import formfields
2020-10-13T07:27:21.718417+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/phonenumber_field/formfields.py", line 1, in <module>
2020-10-13T07:27:21.718418+00:00 app[web.1]: import phonenumbers
2020-10-13T07:27:21.718418+00:00 app[web.1]: ModuleNotFoundError: No module named 'phonenumbers'
2020-10-13T07:27:21.718885+00:00 app[web.1]: [2020-10-13 07:27:21 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-10-13T07:27:21.734657+00:00 app[web.1]: [2020-10-13 07:27:21 +0000] [9] [ERROR] Exception in worker process
2020-10-13T07:27:21.734659+00:00 app[web.1]: Traceback (most recent call last):
2020-10-13T07:27:21.734660+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-10-13T07:27:21.734661+00:00 app[web.1]: worker.init_process()
2020-10-13T07:27:21.734661+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 119, in init_process
2020-10-13T07:27:21.734662+00:00 app[web.1]: self.load_wsgi()
2020-10-13T07:27:21.734662+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2020-10-13T07:27:21.734662+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-10-13T07:27:21.734663+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-10-13T07:27:21.734664+00:00 app[web.1]: self.callable = self.load()
2020-10-13T07:27:21.734664+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-10-13T07:27:21.734664+00:00 app[web.1]: return self.load_wsgiapp()
2020-10-13T07:27:21.734665+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-10-13T07:27:21.734665+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-10-13T07:27:21.734666+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 358, in import_app
2020-10-13T07:27:21.734666+00:00 app[web.1]: mod = importlib.import_module(module)
2020-10-13T07:27:21.734667+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2020-10-13T07:27:21.734667+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-10-13T07:27:21.734668+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2020-10-13T07:27:21.734668+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2020-10-13T07:27:21.734669+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2020-10-13T07:27:21.734669+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2020-10-13T07:27:21.734670+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 783, in exec_module
2020-10-13T07:27:21.734670+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-10-13T07:27:21.734671+00:00 app[web.1]: File "/app/projectname/wsgi.py", line 16, in <module>
2020-10-13T07:27:21.734671+00:00 app[web.1]: application = get_wsgi_application()
2020-10-13T07:27:21.734672+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2020-10-13T07:27:21.734672+00:00 app[web.1]: django.setup(set_prefix=False)
2020-10-13T07:27:21.734673+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
2020-10-13T07:27:21.734673+00:00 app[web.1]: apps.populate(settings.INSTALLED_APPS)
2020-10-13T07:27:21.734674+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
2020-10-13T07:27:21.734674+00:00 app[web.1]: app_config.import_models()
2020-10-13T07:27:21.734674+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
2020-10-13T07:27:21.734675+00:00 app[web.1]: self.models_module = import_module(models_module_name)
2020-10-13T07:27:21.734675+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2020-10-13T07:27:21.734676+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-10-13T07:27:21.734697+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2020-10-13T07:27:21.734697+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2020-10-13T07:27:21.734698+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2020-10-13T07:27:21.734698+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2020-10-13T07:27:21.734698+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 783, in exec_module
2020-10-13T07:27:21.734699+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-10-13T07:27:21.734699+00:00 app[web.1]: File "/app/appname/models.py", line 83, in <module>
2020-10-13T07:27:21.734699+00:00 app[web.1]: from phonenumber_field.modelfields import PhoneNumberField
2020-10-13T07:27:21.734700+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/phonenumber_field/modelfields.py", line 7, in <module>
2020-10-13T07:27:21.734700+00:00 app[web.1]: from phonenumber_field import formfields
2020-10-13T07:27:21.734701+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/phonenumber_field/formfields.py", line 1, in <module>
2020-10-13T07:27:21.734701+00:00 app[web.1]: import phonenumbers
2020-10-13T07:27:21.734701+00:00 app[web.1]: ModuleNotFoundError: No module named 'phonenumbers'
2020-10-13T07:27:21.736172+00:00 app[web.1]: [2020-10-13 07:27:21 +0000] [9] [INFO] Worker exiting (pid: 9)
2020-10-13T07:27:21.999779+00:00 app[web.1]: [2020-10-13 07:27:21 +0000] [4] [INFO] Shutting down: Master
2020-10-13T07:27:21.999967+00:00 app[web.1]: [2020-10-13 07:27:21 +0000] [4] [INFO] Reason: Worker failed to boot.
2020-10-13T07:27:22.139243+00:00 heroku[web.1]: Process exited with status 3
2020-10-13T07:27:22.183525+00:00 heroku[web.1]: State changed from starting to crashed

我想知道为什么应用程序总是崩溃。什么是解决方案。

标签: pythondjangoheroku

解决方案


库的安装部分django-phonenumber-field意味着您必须通过安装库

pip install django-phonenumber-field[phonenumberslite]

这意味着在你的requirements.txt它应该看起来像

django-phonenumber-field[phonenumberslite]==5.0.0

甚至更好,因为所有版本都冻结了:

django-phonenumber-field==5.0.0
phonenumberslite==8.12.11

(根据您的要求,您可能想要更改phonenumberslitephonenumbers,但这取决于您)


推荐阅读