Heroku迁移:ModuleNotFoundError(可疑的静态问题)

时间:2018-10-24 01:16:37

标签: python django heroku static

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

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

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/'。虽然,这可能是无关的问题。

CODE

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

1 个答案:

答案 0 :(得分:1)

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

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

$ pip install pipenv

$ pipenv install -r requirements.txt

$ pipenv lock

Source