Ex51 app_tests AttributeError: module 'app' has no attribute 'config'

I have got the following errors when run nosetests
File “/Users/nbnuser/Development/python_tutorial/hardway/projects/gothonweb/tests/app_tests.py”, line 4, in
app.config[‘TESTING’] = True
AttributeError: module ‘app’ has no attribute ‘config’

My app.py:

from flask import Flask
from flask import render_template
from flask import request

app = Flask(__name__)

@app.route('/hello', methods=['POST', 'GET'])
def index():	
    greeting = 'Hello, World'

    if request.method == "POST":
        name = request.form['name']
        greet = request.form['greet']
        greeting = f"{greet} {name}"
        return render_template("index.html", greeting=greeting)
    else:
        return render_template("hello_form.html")   


if __name__ == "__main__":
    app.run()

This error is actually in your app_tests.py file, and it looks like you didn’t create access to the app variable. When you post code, do this:

[code]
# code here
[/code]

This probably has to do with the fact that in your app_test.py you are not calling the correct location of your app.py file on the import statement to import the flask object app.

My app.py file is in the directory under the gothonweb folder "C:\Users\NserName\Documents\projects\gothonweb\gothonweb’. In this type of situation you would edit your app_test.py to look like this.

from nose.tools import *
from gothonweb.app import app 

The trick here is telling in the from section where your app.py file is.

Yes I know it should have been projects\ex51\gothonweb but I had to mess around to figure out this issue because in the video example it doesn’t look like Zed covers this or gives us a good over view of how his project folder is structured. I hope this helps you on your coding journey

1 Like

Ahhhh yes, now that could also mean that @heidi is doing this:

cd tests
nosetests

Which would mean that @heidi can’t even access the app.py in gothonweb. To fix that, stop doing cd tests, and make sure you CAN do:

ls gothonweb/app.py

It becomes working once I add the line:

[code]
from app import app

[code]

Great, go with that then, but also try to explain “Why” this is working.

@Swervin has it:
If you have created your app.py file within 'gothonweb (as it implies in the book but does not specify), then the code within tests/app_tests.py will be wrong.

The book says:

from app import app

should read:

from gothonweb.app import app

I think your directory structure must be wrong. Here’s mine, so you can compare:

A free service run by Zed A. Shaw for learncodethehardway.org.