【问题标题】:Container Command Fails in Django on Elastic Beanstalk Python 3.7Elastic Beanstalk Python 3.7 上的 Django 中的容器命令失败
【发布时间】:2020-11-07 02:24:35
【问题描述】:

我在 Elastic Beanstalk (Amazon Linux 2) 上使用 Django Python 3.7,但以下命令失败:

container_commands:
  01_migrate:
    command: "pipenv run python ./manage.py migrate"
    leader_only: true
2020-07-17 09:31:57,017 [ERROR] Command 01_migrate (pipenv run python ./manage.py migrate) failed
2020-07-17 09:31:57,017 [ERROR] Error encountered during build of postbuild_0_sarahandanatol: Command 01_migrate failed
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 542, in run_config
    CloudFormationCarpenter(config, self._auth_config).build(worklog)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 260, in build
    changes['commands'] = CommandTool().apply(self._config.commands)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/command_tool.py", line 117, in apply
    raise ToolError(u"Command %s failed" % name)
ToolError: Command 01_migrate failed

【问题讨论】:

  • 嗨。在 AL2 上,如果你想在 python 3 下执行你的代码,我认为应该是python3。你试过python3吗?
  • 还没有,但现在就去! :-)
  • @Marcin 我在使用这个容器命令时遇到了同样的错误:pipenv run python3 ./manage.py migrate 并且由于某种原因,异常仍然显示它使用的是 python 2.7
  • 所以这在 AL1 上工作,但现在当你迁​​移到 AL2 时它停止工作了?
  • @Marcin 是的,你是对的! Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment? 我已经安装了 django,我在命令之前运行 pipenv run

标签: django amazon-web-services amazon-elastic-beanstalk


【解决方案1】:

我尝试在我的沙盒帐户上复制问题。这是 django 的最小版本 只是欢迎屏幕。没有数据库,也没有使用环境变量。

我的Pipfile 也是最小的:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
django = "*"
djangorestframework = "*"

[requires]
python_version = "3.7"

我可以确认使用普通 pipenv 失败。具体来说,我有以下配置文件 在我的.ebextantions

container_commands:
  10_migrate:
    command: |
      pipenv run python ./manage.py migrate

错误消息是关于缺少 Django

但是,对我来说,解决方案如下:

container_commands:
  10_migrate:
    command: |      
      source $PYTHONPATH/activate
      pipenv run python ./manage.py migrate

这会激活 EB 用来安装 Pipfile 依赖项的 python 环境 在执行 pipenv 之前。

下面是一个版本,它也将加载 EB 环境变量,可能 如果您有数据库连接详细信息,则需要运行迁移作业 就这样通过了。

container_commands:
  10_migrate:
    command: |
      export $(cat /opt/elasticbeanstalk/deployment/env | xargs)
      source $PYTHONPATH/activate
      pipenv run python ./manage.py migrate

这是来自 /var/log/cfn-init-cmd.log 的示例输出,显示成功的迁移运行:

20-07-18 04:50:41,615 P3836 [INFO] Command 10_migrate
2020-07-18 04:50:42,969 P3836 [INFO] -----------------------Command Output-----------------------
2020-07-18 04:50:42,969 P3836 [INFO]    cat: /opt/elasticbeanstalk/deployment/env: No such file or directory
2020-07-18 04:50:42,969 P3836 [INFO]    export EB_IS_COMMAND_LEADER="true"
2020-07-18 04:50:42,969 P3836 [INFO]    export HOME="/root"
2020-07-18 04:50:42,969 P3836 [INFO]    export MYVAR="my-eb-env-value"
2020-07-18 04:50:42,969 P3836 [INFO]    export OLDPWD
2020-07-18 04:50:42,970 P3836 [INFO]    export PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
2020-07-18 04:50:42,970 P3836 [INFO]    export PWD="/var/app/staging"
2020-07-18 04:50:42,970 P3836 [INFO]    export PYTHONPATH="/var/app/venv/staging-LQM1lest/bin"
2020-07-18 04:50:42,970 P3836 [INFO]    export SHLVL="4"
2020-07-18 04:50:42,970 P3836 [INFO]    export _="/bin/jq"
2020-07-18 04:50:42,970 P3836 [INFO]    Courtesy Notice: Pipenv found itself running within a virtual environment, so it will automatically use that environment, instead of creating its own for any project. You can set PIPENV_IGNORE_VIRTUALENVS=1 to force pipenv to ignore that environment and create its own instead. You can set PIPENV_VERBOSITY=-1 to suppress this warning.
2020-07-18 04:50:42,970 P3836 [INFO]    Operations to perform:
2020-07-18 04:50:42,970 P3836 [INFO]      Apply all migrations: admin, auth, contenttypes, sessions
2020-07-18 04:50:42,970 P3836 [INFO]    Running migrations:
2020-07-18 04:50:42,970 P3836 [INFO]      Applying contenttypes.0001_initial... OK
2020-07-18 04:50:42,970 P3836 [INFO]      Applying auth.0001_initial... OK
2020-07-18 04:50:42,970 P3836 [INFO]      Applying admin.0001_initial... OK
2020-07-18 04:50:42,970 P3836 [INFO]      Applying admin.0002_logentry_remove_auto_add... OK
2020-07-18 04:50:42,970 P3836 [INFO]      Applying admin.0003_logentry_add_action_flag_choices... OK
2020-07-18 04:50:42,971 P3836 [INFO]      Applying contenttypes.0002_remove_content_type_name... OK
2020-07-18 04:50:42,971 P3836 [INFO]      Applying auth.0002_alter_permission_name_max_length... OK
2020-07-18 04:50:42,971 P3836 [INFO]      Applying auth.0003_alter_user_email_max_length... OK
2020-07-18 04:50:42,971 P3836 [INFO]      Applying auth.0004_alter_user_username_opts... OK
2020-07-18 04:50:42,971 P3836 [INFO]      Applying auth.0005_alter_user_last_login_null... OK
2020-07-18 04:50:42,971 P3836 [INFO]      Applying auth.0006_require_contenttypes_0002... OK
2020-07-18 04:50:42,971 P3836 [INFO]      Applying auth.0007_alter_validators_add_error_messages... OK
2020-07-18 04:50:42,971 P3836 [INFO]      Applying auth.0008_alter_user_username_max_length... OK
2020-07-18 04:50:42,971 P3836 [INFO]      Applying auth.0009_alter_user_last_name_max_length... OK
2020-07-18 04:50:42,971 P3836 [INFO]      Applying auth.0010_alter_group_name_max_length... OK
2020-07-18 04:50:42,971 P3836 [INFO]      Applying auth.0011_update_proxy_permissions... OK
2020-07-18 04:50:42,971 P3836 [INFO]      Applying sessions.0001_initial... OK

【讨论】:

  • source $PYTHONPATH/activate 在经历了几个小时的挫折之后对我来说是关键。
猜你喜欢
  • 2020-10-08
  • 2016-02-27
  • 2021-02-17
  • 2014-03-10
  • 2020-12-13
  • 2017-02-13
  • 2013-12-20
  • 2017-08-03
  • 2015-09-20
相关资源
最近更新 更多