pythonflaskpython-3.6flask-sqlalchemyflask-uploads

flask_uploads: ImportError: cannot import name 'secure_filename'


I want to create a form that allows to send a picture with a description using flask forms. I tried to use this video: https://www.youtube.com/watch?v=Exf8RbgKmhM

but I had troubles when launching app.py:

➜  website git:(master) ✗ python3.6 app.py
Traceback (most recent call last):
  File "app.py", line 10, in <module>
    from flask.ext.uploads import UploadSet, configure_uploads, IMAGES
ModuleNotFoundError: No module named 'flask.ext'

I had to replace flask.ext.uploads by flask_uploads but now I get:

Traceback (most recent call last):
  File "app.py", line 10, in <module>
    from flask_uploads import UploadSet, configure_uploads, IMAGES
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask_uploads.py", line 26, in <module>
    from werkzeug import secure_filename, FileStorage
ImportError: cannot import name 'secure_filename'

My imports and config looks like this:

from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from flask import Flask, session, render_template, url_for, redirect, flash, request
from wtforms import Form, fields,TextField, StringField, PasswordField, BooleanField,validators
from wtforms.validators import InputRequired, Email, Length, DataRequired
from flask_wtf import FlaskForm
from flask_uploads import UploadSet, configure_uploads, IMAGES
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user

I couldn't solve this issue, do you have any idea of what can I do ?


Solution

  • In flask_uploads.py

    Change

    from werkzeug import secure_filename,FileStorage
    

    to

    from werkzeug.utils import secure_filename
    from werkzeug.datastructures import  FileStorage