AttributeError: module 'werkzeug.utils' has no attribute 'send_from_directory'

I have followed the Flask Uploading Files documentation at https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/, making a file called 'upload.py':

import os
from flask import Flask, flash, request, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename

UPLOAD_FOLDER = '/opt/tabeteb/aiomed/dss/uploads/medical_imaging/cxr'
ALLOWED_EXTENSIONS = {'jpg', 'jpeg', 'png', 'gif'}

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file', filename=filename))
    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form method=post enctype=multipart/form-data>
      <input type=file name=file>
      <input type=submit value=Upload>
    </form>
    '''

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

Running the built-in server:

$ export FLASK_APP=upload.py
$ flask run --host=0.0.0.0

is OK, but uploading a file returns an Internal server error on a such address (http://my_server_ip:5000/uploads/file_name.png) with an AttributeError regarding module werkzeug.utils with send_from_directory shown on server log:

[2020-11-25 10:13:43,414] ERROR in app: Exception on /uploads/file_name.png [GET]
Traceback (most recent call last):
  File "/opt/tabeteb/aiomed/dss/venv/lib/python3.6/site-packages/flask/app.py", line 2022, in wsgi_app
    response = self.full_dispatch_request()
  File "/opt/tabeteb/aiomed/dss/venv/lib/python3.6/site-packages/flask/app.py", line 1537, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/opt/tabeteb/aiomed/dss/venv/lib/python3.6/site-packages/flask/app.py", line 1535, in full_dispatch_request
    rv = self.dispatch_request()
  File "/opt/tabeteb/aiomed/dss/venv/lib/python3.6/site-packages/flask/app.py", line 1521, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/opt/tabeteb/aiomed/dss/upload.py", line 45, in uploaded_file
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
  File "/opt/tabeteb/aiomed/dss/venv/lib/python3.6/site-packages/flask/helpers.py", line 657, in send_from_directory
    return werkzeug.utils.send_from_directory(
AttributeError: module 'werkzeug.utils' has no attribute 'send_from_directory'

(EDITED) Using:

  • Flask (2.0.0.dev0)
  • Werkzeug (1.0.1)
  • Ubuntu server 18.04.5

1 answer

  • answered 2020-11-25 18:14 Puneet Jain

    Your code is all OK, you just have to use flask version 1.1.2, then you got no error.