pythondockerflaskoptparse

Flask stops at OptionParser.parse_args() when running it using docker


I am using flask to create a Rest API, and there, it loads a pre-trained model, renders the graphical main page , and then get some input directories to feed data to classification.

when I run the code on my local machine, it all works fine. But when I use docker to run the app.py file(where the API is and loads the trained model), it gets stuck at OptionParser.parse_args(). Is there any way that I can handle this problem and keep using optparse? Here is how my code looks like.

app.py:

from flask import Flask, request, jsonify, render_template, redirect, url_for
import os
import sys

sys.path.insert(0, '/docker_dir_to_project/')
sys.path.insert(0, '/docker_dir_to_project/deploy')

import model_backend

from optparse import OptionParser
app = Flask(__name__)
print('\n\n\n\nopened flask app\n\n\n')

# some initialization, here I just load the config and load the model
class my_app:
    def __init__(self):
        # some directories
        print('init')
        self.deploy_dir = os.path.dirname(os.path.abspath(__file__))
        # it gets stuck in this method:
        self.opt = self._create_options()
        
        print('2')
        # building the model, optparse is used in this method again.
        self.recognizer = model_backend.classifier(self.opt,self.device)

    # uses option parser, and it gets stuck here
    def _create_options(self):
        parser = OptionParser()
        print('11')  # will be printed
        print('12')  # will be printed
        (opt, args) = parser.parse_args()  # with docker it gets stuck here, 
        print('13')  # won't be printed

        opt.some_settings = some_settings

        return opt


# the rest of a normal flask application



print('BEFORE INIT')
recognizer_system_ = my_app()
print('after init')

# the api
@app.route('/', methods=['GET', 'POST'])
def Main_page():
    print('main page')
    return render_template('Main_page.html')

# this file is not run
if __name__ == '__main__':
    print('\n\n\nmain file in app\n\n\n')
    app.run(host='0.0.0.0')

The dockerfile is as followed:

FROM python:3.7

#
RUN apt-get update
ADD all_required_files /all_required_files_on_docker
ENV FLASK_DEBUG=1
WORKDIR /docker_dir_to_project/deploy
CMD [ "flask", "run", "--host=0.0.0.0"]

and the bash file which I run with sudo bash command is as followed:

dockerize.sh:

#!/bin/bash

docker build -f ./deploy/Dockerfile -t sr .
docker run -p 5000:5000 sr

When I run normally on local host (no docker), it works fine. But when I use the dockerize.sh script, it cannot load the main page, and the output on console is as followed:

Usage: flask [options]

flask: error: no such option: --host

opened flask app


BEFORE INIT
init
11
12

Which means, it gets stuck at app.py -> my_app -> _create_options -> (opt, args) = parser.parse_args()

Does anyone have any Idea how to solve it? I use the optparse later in the model to load parameters, and prefer to keep using it if possible. Thank you all in advance for your time.


Solution

  • I could fix the problem by using argparse package instead of optparse, and replacing

    parser = OptionParser()
    (opt, args) = parser.parse_args()
    
    

    with

    parser = argparse.ArgumentParser()
    opt, _ = parser.parse_known_args()