gpt4 book ai didi

python - Flask CLI 在通过 docker-compose 运行时抛出 'OSError: [Errno 8] Exec format error'

转载 作者:IT老高 更新时间:2023-10-28 12:37:31 24 4
gpt4 key购买 nike

我正在运行一个带有 Custom Script 的 Flask 应用程序。 .或者尝试,无论如何。

我在 Windows 10 上,应用程序应该使用以下命令在 linux Docker 容器中运行:

docker-compose up api

Docker-compose 是 1.23.2 版。在dockerfile中,api服务通过命令运行:

command: python manage.py run --host "0.0.0.0" --with-threads

当它尝试启动时,我看到了异常

OSError: [Errno 8] Exec format error: '/api/manage.py'

我最初认为这将是可怕的 Windows 行尾,再次来找我,但在我所有的源文件上运行 dos2unix 并没有解决问题。

我怎样才能避免这个错误?


ma​​nage.py

    import click
from flask.cli import FlaskGroup

from my_app_api import create_app


def create_my_app(info):
return create_app()


@click.group(cls=FlaskGroup, create_app=create_my_app)
def cli():
pass


if __name__ == "__main__":
cli()

完整追溯

api_1          | Traceback (most recent call last):
api_1 | File "manage.py", line 22, in <module>
api_1 | cli()
api_1 | File "/usr/local/lib/python3.6/site-packages/click/core.py", line 764, in __call__
api_1 | return self.main(*args, **kwargs)
api_1 | File "/usr/local/lib/python3.6/site-packages/flask/cli.py", line 380, in main
api_1 | return AppGroup.main(self, *args, **kwargs)
api_1 | File "/usr/local/lib/python3.6/site-packages/click/core.py", line 717, in main
api_1 | rv = self.invoke(ctx)
api_1 | File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1137, in invoke
api_1 | return _process_result(sub_ctx.command.invoke(sub_ctx))
api_1 | File "/usr/local/lib/python3.6/site-packages/click/core.py", line 956, in invoke
api_1 | return ctx.invoke(self.callback, **ctx.params)
api_1 | File "/usr/local/lib/python3.6/site-packages/click/core.py", line 555, in invoke
api_1 | return callback(*args, **kwargs)
api_1 | File "/usr/local/lib/python3.6/site-packages/click/decorators.py", line 64, in new_func
api_1 | return ctx.invoke(f, obj, *args, **kwargs)
api_1 | File "/usr/local/lib/python3.6/site-packages/click/core.py", line 555, in invoke
api_1 | return callback(*args, **kwargs)
api_1 | File "/usr/local/lib/python3.6/site-packages/flask/cli.py", line 438, in run_command
api_1 | use_debugger=debugger, threaded=with_threads)
api_1 | File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 988, in run_simple
api_1 | run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
api_1 | File "/usr/local/lib/python3.6/site-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
api_1 | sys.exit(reloader.restart_with_reloader())
api_1 | File "/usr/local/lib/python3.6/site-packages/werkzeug/_reloader.py", line 176, in restart_with_reloader
api_1 | exit_code = subprocess.call(args, env=new_environ, close_fds=False)
api_1 | File "/usr/local/lib/python3.6/subprocess.py", line 287, in call
api_1 | with Popen(*popenargs, **kwargs) as p:
api_1 | File "/usr/local/lib/python3.6/subprocess.py", line 729, in __init__
api_1 | restore_signals, start_new_session)
api_1 | File "/usr/local/lib/python3.6/subprocess.py", line 1364, in _execute_child
api_1 | raise child_exception_type(errno_num, err_msg, err_filename)
api_1 | OSError: [Errno 8] Exec format error: '/api/manage.py'

最佳答案

看起来您的 api/manage.py 没有 shebang ([Wikipedia]: Shebang (Unix)),所以默认(当前)命令处理器(一个 shell - 通常是 bash)正在尝试运行它,但(显然)失败了。

要纠正问题,请添加 shebang(在文件的开头,确保您的编辑器添加 Nix 样式的行尾(\n, 0x0A, LF)):

  • 默认Python安装:

    #!/usr/bin/env python
    • 变体(明确指定Python 3):

      #!/usr/bin/env python3
  • 自定义Python安装:

    #!/full/path/to/your/custom/python/executable

请注意,您还需要文件的 exec 权限(chmod +x api/manage.py)。

例子:

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q055271912]> ~/sopr.sh
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[064bit prompt]> ls
code00.py code01.py
[064bit prompt]>
[064bit prompt]> cat code00.py
print("This is:", __file__)

[064bit prompt]> python3 -c "import os, subprocess;subprocess.Popen(os.path.join(os.getcwd(), \"code00.py\")).communicate()"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/cygdrive/e/Work/Dev/StackOverflow/q055271912/code00.py'
[064bit prompt]>
[064bit prompt]> cat code01.py
#!/usr/bin/env python3

print("This is:", __file__)

[064bit prompt]> python3 -c "import os, subprocess;subprocess.Popen(os.path.join(os.getcwd(), \"code01.py\")).communicate()"
This is: /cygdrive/e/Work/Dev/StackOverflow/q055271912/code01.py

另一种方法是运行解释器,后跟文件名,但我不知道如何从 Flask 执行此操作 - 实际上这需要修补 Werkzeug (_reloader.py:_get_args_for_reloading),但这只是一个蹩脚的解决方法(gainarie) - 见下文。



更新#0

查看@AxelGrytt 的回答,事实证明这是一个已知问题:[GitHub]: pallets/werkzeug - 0.15.0 causes OSError: [Errno 8] Exec format error: in Docker for Windows (嗯,与这个问题在同一天提交(发布后 2 天):))。

所以,我上面说的是正确的,但值得一提的是,还有另一种修复方法:删除文件的 exec 权限:

chmod -x api/manage.py

根据 Werkzeug 作者的说法,从现在开始,这是理想的行为(也适用于 v0.15.2 em>):

  • 一个文件带有 exec权限集,应该也有一个shebang
  • 文件没有 shebang不应该拥有exec权限集

关于python - Flask CLI 在通过 docker-compose 运行时抛出 'OSError: [Errno 8] Exec format error',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55271912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com