David
David

Reputation: 10738

how to use absolute paths in flask app?

I'm attempting to learn python and can't seem to understand how python handles imports. I'm experimenting with a Flask app and i can get it to run with relative urls, but i want to use absolute urls as an exercise to learn about imports.

I've set my PYTHONPATH to my app directory inside my project but that doesn't seem to do anything.

Here's my setup. I've got app/__init.py on the left and app/mod_users/controllers.py on the right. My run.py is a file with one line, from app import app.

As you can see on line 17, it doesn't like the absolute import. Could this be because my app variable is named app and so is the import path?

enter image description here

Upvotes: 0

Views: 1893

Answers (1)

David
David

Reputation: 10738

After some experimenting thanks to @downshift I think i've figured it out. 4 steps get me to to state i want.

  1. Add this line to the top of my app/__init.py

    from __future__ import absolute_import
    
  2. export PYTHONPATH=./

  3. export FLASK_APP=app (and FLASK_DEBUG=1 for live reload)

  4. Change run.py to

    from app import app
    app.run()
    

If i do the above i can use both flask run and python run.py and they work the same. Woo hoo!

Upvotes: 1

Related Questions