AttributeError: module 'werkzeug.utils' has no attribute 'send_from_directory'
AttributeError: module 'werkzeug.utils' has no attribute 'send_from_directory'
我遵循了 https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/ 上的 Flask 上传文件文档,创建了一个名为 '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)
运行内置服务器:
$ export FLASK_APP=upload.py
$ flask run --host=0.0.0.0
没问题,但是上传文件 returns 在这样的地址 (http://my_server_ip:5000/uploads/file_name.png) 带有关于模块 werkzeug.utils 的 AttributeError,服务器日志上显示 send_from_directory:
[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'
(已编辑)
使用:
- 烧瓶 (2.0.0.dev0)
- Werkzeug (1.0.1)
- Ubuntu 服务器 18.04.5
你的代码没问题,你只需要使用1.1.2版本的flask,就没有错误了。
试试吧!
pip卸载工具
然后
pip 安装工具
我遵循了 https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/ 上的 Flask 上传文件文档,创建了一个名为 '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)
运行内置服务器:
$ export FLASK_APP=upload.py
$ flask run --host=0.0.0.0
没问题,但是上传文件 returns 在这样的地址 (http://my_server_ip:5000/uploads/file_name.png) 带有关于模块 werkzeug.utils 的 AttributeError,服务器日志上显示 send_from_directory:
[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'
(已编辑) 使用:
- 烧瓶 (2.0.0.dev0)
- Werkzeug (1.0.1)
- Ubuntu 服务器 18.04.5
你的代码没问题,你只需要使用1.1.2版本的flask,就没有错误了。
试试吧! pip卸载工具 然后 pip 安装工具