我正在使用自定义脚本运行Flask应用程序。还是尝试,无论如何。
我在Windows 10上,应使用以下命令在Linux Docker容器中运行应用程序:
docker-compose up api
Docker-compose是version 1.23.2。在dockerfile中,api服务通过以下命令运行:
version 1.23.2
api
command: python manage.py run --host "0.0.0.0" --with-threads
在尝试启动时,我看到了异常
OSError: [Errno 8] Exec format error: '/api/manage.py'
我最初以为这将是“可怕的Windows Line Ends”,再次给我,但是dos2unix在我的所有源文件上运行都无法解决问题。
如何避免此错误?
manage.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)):
#!/usr/bin/env python
#!/usr/bin/env python3
#!/full/path/to/your/custom/python/executable
请注意,你还需要对该文件()具有执行权限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 *** [prompt]> ls code0.py code1.py [prompt]> [prompt]> cat code0.py print("This is:", __file__) [prompt]> python3 -c "import os, subprocess;subprocess.Popen(os.path.join(os.getcwd(), \"code0.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/code0.py' [prompt]> [prompt]> cat code1.py #!/usr/bin/env python3 print("This is:", __file__) [prompt]> python3 -c "import os, subprocess;subprocess.Popen(os.path.join(os.getcwd(), \"code1.py\")).communicate()" This is: /cygdrive/e/Work/Dev/StackOverflow/q055271912/code1.py