【问题标题】:"Error while running '$ python manage.py collectstatic --noinput'" after changing database on django“在 django 上更改数据库后,运行 '$ python manage.py collectstatic --noinput' 时出错”
【发布时间】:2019-08-15 06:40:28
【问题描述】:

我是 Django 的初学者,如果我遗漏了一些必要的信息,请原谅我。我最近在 settings.py 上修改了我的数据库,但我在运行时不断收到错误

    git push heroku master

错误块看起来像:

    Counting objects: 22, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (22/22), done.
Writing objects: 100% (22/22), 4.84 KiB | 825.00 KiB/s, done.
Total 22 (delta 12), reused 0 (delta 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.6.8
remote:        Learn More: https://devcenter.heroku.com/articles/python-runtimes
remote: -----> Installing requirements with pip
remote: 
remote: -----> $ python manage.py collectstatic --noinput
remote:        Traceback (most recent call last):
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 20, in <module>
remote:            import psycopg2 as Database
remote:        ModuleNotFoundError: No module named 'psycopg2'
remote:        During handling of the above exception, another exception occurred:
remote:        Traceback (most recent call last):
remote:          File "manage.py", line 15, in <module>
remote:            execute_from_command_line(sys.argv   )
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line  
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
remote:            django.setup()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
remote:            apps.populate(settings.INSTALLED_APPS)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
remote:            app_config.import_models()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
remote:            self.models_module = import_module(models_module_name)
remote:          File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
remote:            return _bootstrap._gcd_import(name[level:], package, level)
remote:          File "<frozen importlib._bootstrap>", line 994, in _gcd_import
remote:          File "<frozen importlib._bootstrap>", line 971, in _find_and_load
remote:          File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
remote:          File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
remote:          File "<frozen importlib._bootstrap_external>", line 678, in exec_module
remote:          File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in <module>
remote:            from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
remote:            class AbstractBaseUser(models.Model):
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 101, in __new__
remote:            new_class.add_to_class('_meta', Options(meta, app_label))
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 305, in add_to_class
remote:            value.contribute_to_class(cls, name)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/options.py", line 203, in contribute_to_class
remote:            self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__
remote:            return getattr(connections[DEFAULT_DB_ALIAS], item)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 202, in __getitem__
remote:            backend = load_backend(db['ENGINE'])
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 110, in load_backend
remote:            return import_module('%s.base' % backend_name)
remote:          File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
remote:             return _bootstrap._gcd_import(name[level:], package, level)
remote:           File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/  base.py", line 24, in <module>
remote:            raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
remote:        django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module    named 'psycopg2'
remote: 
remote:  !     Error while running '$ python manage.py collectstatic --noinput'.
remote:        See traceback above for details.
remote: 
remote:        You may need to update application code to resolve this error.
remote:        Or, you can disable collectstatic for this application:
remote: 
remote:           $ heroku config:set DISABLE_COLLECTSTATIC=1
remote: 
remote:        https://devcenter.heroku.com/articles/django-assets
remote:  !     Push rejected, failed to compile Python app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !   Push rejected to ewbunlv.
remote: 
To https://git.heroku.com/ewbunlv.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/ewbunlv.git'

这是我更改的 settings.py 的一部分(最初是 Django 的默认数据库)

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

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2', #Used 'django.db.backends.postgresql' as well
            'NAME': 'myname',
            USER': 'user',
            'PASSWORD': 'password',
            'HOST': 'host',
            'PORT': '5432',
        }
    }

我的要求.txt:

    awscli==1.16.91
boto==2.49.0
boto3==1.9.81
botocore==1.12.81
certifi==2018.11.29
chardet==3.0.4
colorama==0.3.9
dj-database-url==0.5.0
Django==2.1.4
django-cleanup==3.0.1
django-s3-folder-storage==0.5
django-storages==1.7.1
docutils==0.14
gunicorn==19.9.0
heroku==0.1.4
idna==2.8
jmespath==0.9.3
Pillow==5.3.0
pyasn1==0.4.5
python-dateutil==2.7.5
pytz==2018.7
PyYAML==3.13
requests==2.21.0
rsa==3.4.2
s3transfer==0.1.13
six==1.12.0
urllib3==1.24.1
whitenoise==4.1.2

我跑了

    heroku run python manage.py collectstatic --dry-run --noinput

它工作得很好,没有任何错误。我已经为此工作了一段时间,但无济于事。

它在本地也可以正常工作,但是当我尝试将其转换为生产时,我收到了上面提到的错误。我错过了什么?

【问题讨论】:

  • settings.py 中的那个部分是否准确?有一个明显的引用错误应该会阻止您的应用程序运行(查看语法突出显示)。
  • 此外,您的依赖项中似乎不包含psycopg2。为什么不是?您显然是在尝试使用它。
  • 您是在本地使用 PostgreSQL,还是仅在 Heroku 上使用?在您的所有环境(包括您的开发环境)中使用相同的数据库提供程序通常是一个非常的好主意。库不匹配是众多原因之一。

标签: python django git heroku


【解决方案1】:
  1. 在部署期间禁用 collectstatic
heroku config:set DISABLE_COLLECTSTATIC=1
  1. 部署
git push heroku master (or git push heroku <branch>:master if not on master)
  1. 运行迁移
heroku run python manage.py migrate
  1. 使用 bower 运行 collectstatic
heroku run 'bower install --config.interactive=false;grunt prep;python manage.py collectstatic --noinput'
  1. 为未来的部署启用集体静态
heroku config:unset DISABLE_COLLECTSTATIC
  1. 再次运行 collectstatic
heroku run python manage.py collectstatic

【讨论】:

    【解决方案2】:

    就像 Chris、phd 和 Selcuk 提到的那样,我似乎忽略了 requirements.txt 文件中的一些必要部分。我用过

        pip freeze
    

    然后再次复制/粘贴到 requirements.txt 文件中,这次成功了。

    【讨论】:

    • 你知道你可以通过pip freeze &gt; requirements.txt 将 pip freeze 的输出直接打印到 requrements.txt 中吗?
    猜你喜欢
    • 2017-05-21
    • 2020-10-17
    • 2016-02-01
    • 2017-05-30
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    相关资源
    最近更新 更多