Refering to a Directory in a Flask App Doesn't Work Unless the Path Is Absolute

Refering to a directory in a Flask app doesn't work unless the path is absolute

In Python (and most languages), where the code resides in a package is different than what the working directory is when running a program. All relative paths are relative to the current working directory, not the code file it's written in. So you would use the relative path nltk_data/ even from a blueprint, or you would use the absolute path and leave no ambiguity.

The root_path attribute on an app (or blueprint) refers to the package directory for the app (or blueprint). Join your relative path to that to get the absolute path.

resource_path = os.path.join(app.root_path, 'enltk_data')

There's probably no reason to be appending this folder every time you call a view. I'm not familiar with nltk specifically, but there's probably a way to structure this so you set up the data path once when you create your app.


project    /    app    /    blueprint
/ data

^ join with root_path to get here
^ app.root_path always points here, no matter where cwd is
^ current working directory

Query regarding url_for in Flask

Use app.url_root to get a path relative to your code.

wordlist_path = os.path.join(app.root_path, "static/data/wordlist.txt")

with open(wordlist_path) as f:
...

Flask can't find applications file

I have solved my problem, after finding this post here Refering to a directory in a Flask app doesn't work unless the path is absolute.

What I take from this is that the file path has to be absolute from the Flask applications root folder, in this case "my_flask_site" is the root folder and adding the full file path solved the problem.

Flask and running a script on server VS running in terminal : Different results?

I was able to fix this issue by simply passing the flask app.root_path variable into my python script and adding it in front of 'reqs/model/en-us/en-us' so I guess I needed to have an absolute path instead of a relative one.

Flask Error: The file/path provided does not appear to exist although the file does exist

This situation occurs when you have an ImportError which is not propagated through to your terminal. Check all of your files for invalid import statements, fix them, and the error should disappear.

EDIT 2017-04-02:
@Michael pointed out that my reference now placed under the tag "OLD MESSAGE PART 2" is incorrect. I don't know how this mistake came to be, but I've found a very recent post on the Flask Github where they reference the commit that should have fixed the issue on Dec 30th of 2016. Probably at that time I was indeed running an older flask version.

OLD MESSAGE PART 2:
This issue is discussed on the Flask Github, though I am unsure as to when and even whether it has actually been fixed, since I still encounter the error today even though I downloaded Flask after the merge of the fix described on that page (12 Aug 2016).

Flask/Python: creating zip file from absolute path fails - no such file or directory

Turns out my initial guess was right, and one only needs to use writestr() instead of write(), in addition to reading the file with an http request.
Here is the full working code:

import zipfile
import io
import requests
from flask import request, send_file

def download_multiple_resources():
if request.method == "POST":
memory_file = io.Bytes.IO()
with zipfile.ZipFile(memory_file, 'w', zipfile.ZIP_STORED) as zf:
for res in request.form:
f = requests.get(res, allow_redirects=True)
zf.writestr('your_chosen_filename', f.content)
memory_file.seek(0)

return send_file(memory_file, mimetype='application/zip', as_attachment=True, attachment_filename="download.zip")

Extra note: If you are working on an external server, like me, and use this function to let logged-in users download files, be sure that the credentials of the users are passed to the request.get() call to authorize the download. This in my case has been done using the option headers={'Authorization': user.apiKey}, but this might depend on your framework/application.



Related Topics



Leave a reply



Submit