0

I'm using send_from_directory in flask to serve some static js assets. The app structure looks like this:

/BaseFolder
    /App
        __init__.py
        /js/
            jquery.js
    run.py
    /js/
        jquery.js

The __init__.py is responsible for most of the app management and has the following:

app = Flask(__name__)
[...]
@app.route('/js/<path:path>')
def send_js(path):
    return send_from_directory('js',path)
[...]
def go():
    [...]
    app.run()

The app itself kicks off from the run.py file which runs the go function in the file above. (this essentially mirrors the app setup of the app in Flask Web Development so it should be a typical use case for Flask.)

Theoretically, the send_js function should return a file from either the App/js/ or the js/ directory. However, removing either of these files causes the application to 404. (The app serves the contents of the lower one; even touch js/jquery.js in the upper folder is enough to make things work.)

Why does this happen?

2
  • To understand why is that happening just add the import os; print os.getcwd() right before the send_from_directory call. That will give you a current working directory for your python process.
    – Boris
    Commented Jul 14, 2015 at 20:35
  • Ah, something weird is happening there, but changing the directory in go() seemed to change it.
    – argentage
    Commented Jul 14, 2015 at 20:43

1 Answer 1

0

Since the application runs from run.py, it thinks that its current directory is at /BaseFolder, but the application expects that this directory is at /BaseFolder/App.

Adding os.chdir("app") to run.py fixes this. It doesn't explain why the app expects different paths at different locations, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.