Search code examples
pythonflaskflask-wtforms

Cannot access app.instance_path in Flask


I am trying to implement a file upload function in my Flask app, but then I try:

f.save(os.path.join(app.instance_path, 'imgs', filename))

within my class, I keep getting errors. The first time, I got:

NameError: name 'app' is not defined

Then I tried importing app with import app and got the following:

AttributeError: module 'app' has no attribute 'instance_path'

I am using Flask-Foundation as a framework, and have a Flask application factory creating my app (using the name app). Using Flask version 0.12.2.

Should I specify the instance_path in my settings.py file? However, as the documentation says:

You can either explicitly provide the path of the instance folder when creating the Flask application or you can let Flask autodetect the instance folder.

So I figured I do not need to specify it, and something with my naming might be wrong.... any ideas?

Thanks! Daniel


Solution

  • Yes, fixed by correcting the import:

    import appname.app
    

    Then:

    f.save(os.path.join(appname.app.instance_path, 'imgs', filename))
    

    (of course appname should be whatever you app is named...)