Solved: NameError: name ‘_name_’ is not defined in docker python program

While learning docker, i encountered below error when i had written python sample program.

Error encountered

$ docker-compose up
Creating docker_product-service_1 ... done
Attaching to docker_product-service_1
product-service_1  | Traceback (most recent call last):
product-service_1  |   File "api.py", line 16, in 
product-service_1  |     if _name_ == '_main_':
product-service_1  | NameError: name '_name_' is not defined
docker_product-service_1 exited with code 1

 

Solution
To resolve this issue, we need to correct the python script code and put two underscores
instead of one.

### Faulty Program line
if _name_ == '_main_':
        app.run(host='0.0.0.0', port=80, debug=True)

### Correct it by adding double underscores below.
### Make sure you make changes in any other part of the program.
if __name__ == '__main__':
        app.run(host='0.0.0.0', port=80, debug=True)

After correcting above line when you try to execute the program it should go well.

For example:- In my case, it went well this time.

$ docker-compose up
Starting docker_product-service_1 ... done
Attaching to docker_product-service_1
product-service_1  |  * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
product-service_1  |  * Restarting with stat
product-service_1  |  * Debugger is active!
product-service_1  |  * Debugger PIN: 304-853-752

Leave a Reply

Your email address will not be published.