【问题标题】:Heroku migrate: ModuleNotFoundError (suspect issue with static)Heroku 迁移:ModuleNotFoundError(静态的可疑问题)
【发布时间】:2018-10-24 01:16:36
【问题描述】:

我遇到了与here、here 和here 相同的问题。但是,我尝试了在每个查询中给出的答案,但无济于事。

在尝试部署应用程序时,使用以下命令:

heroku run python manage.py migrate

错误

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 224, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 36, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 14, in <module>
    from django.db.migrations.autodetector import MigrationAutodetector
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/autodetector.py", line 11, in <module>
    from django.db.migrations.questioner import MigrationQuestioner
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/questioner.py", line 9, in <module>
    from .loader import MigrationLoader
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/loader.py", line 8, in <module>
    from django.db.migrations.recorder import MigrationRecorder
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 9, in <module>
    class MigrationRecorder:
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 22, in MigrationRecorder
    class Migration(models.Model):
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 87, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 249, in get_containing_app_config
    self.check_apps_ready()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 131, in check_apps_ready
    settings.INSTALLED_APPS
  File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 57, in __getattr__
    self._setup(name)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 107, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/app/mapping_data/settings.py", line 2, in <module>
    from decouple import config
ModuleNotFoundError: No module named 'decouple'

我怀疑问题是由我如何部署 static 引起的。特别是因为命令:python manage.py collectstatic 返回以下错误:FileNotFoundError: [Errno 2] No such file or directory at: '/mnt/project/static/'。虽然,这可能是一个不相关的问题。

代码

requirements.txt

python-decouple==3.1

settings.py

INSTALLED_APPS = [
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
# The absolute path to the directory where collectstatic will collect static files for deployment
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
PROJECT_PATH = os.path.abspath(os.path.dirname(__name__))
# The URL to use when referring to static files (where they will be served from)
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, 'static'),
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
ROOT_URLCONF = 'mapping_data.urls'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
MEDIA_ROOT = (
BASE_DIR
)

Pipfile

[[source]]

url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[packages]

django = "==2.1.2"

[dev-packages]

结构

.git
__pycache__
htmlcov
mapping_data
     __pycache__
     static
         .keep
     __init__.py
     settings.py
     urls.py
     wsgi.py
mapping_twitter
     __pycache__
     migrations
     static
         css
             styles.css
         mapping_twitter
             histogram.png
     templates
     tests
     __init__.py
     admin.py
     apps.py
     forms.py
     models.py
     urls.py
     views.py
.coverage
.editorconfig
.env
.gitignore
db.sqlite3
ghostdriver.log
manage.py
Pipfile
Pipfile.lock
Procfile
requirements.txt
runtime.txt

【问题讨论】:

  • 它们似乎是不同的问题。可以发一下Pipfile的内容吗?
  • 我现在已经收录了Pipfile
  • 只是一个疯狂的猜测,但当有 Pipfile 时,您部署的 Heroku 堆栈可能会忽略 requirements.txt。您可以尝试在[packages] 下添加python-decouple = "==3.1" 吗?
  • 我听从了您的建议,但仍然遇到完全相同的错误。如果有任何帮助,我已经在上面的查询中包含了完整的 Traceback。
  • 谢谢@Selcuk,问题是,正如您在评论中可能假设的那样,在Pipfile 中将python-decouple 添加到[packages] 后,我没有运行$ pipenv lock /跨度>

标签: python django heroku static


【解决方案1】:

正如@Selcuk 所说,问题是Pipfile 被忽略了requirements.txt。

从基于requirements.txt 的依赖项转换为pipenv 所需的步骤是:

$ pip install pipenv

$ pipenv install -r requirements.txt

$ pipenv lock

Source

【讨论】:

    猜你喜欢
    • 2012-06-05
    • 1970-01-01
    • 2021-07-13
    • 2015-05-25
    • 2018-10-23
    • 2014-02-13
    • 2019-04-16
    • 2011-10-19
    • 1970-01-01
    相关资源
    最近更新 更多