51

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 ?

Santeau
  • 839
  • 3
  • 13
  • 23

6 Answers6

144

In flask_uploads.py

Change

from werkzeug import secure_filename,FileStorage

to

from werkzeug.utils import secure_filename
from werkzeug.datastructures import  FileStorage
Nandu Raj
  • 2,072
  • 9
  • 20
true man
  • 1,456
  • 1
  • 5
  • 2
36

According to this issue, it is a bug related to the current version 1.0.0 of workzeug. It's merged but not yet published in pypi. The workaround know until now is to downgrade from werkzeug=1.0.0 to werkzeug==0.16.0

So for do that you just need run the command:

pip install -U Werkzeug==0.16.0

Looking in the release notes from werkzeug there is a version 0.16.1, but in bug report there is no evidence that using that version could be of any help.

Danizavtz
  • 3,166
  • 4
  • 24
  • 25
  • 3
    Thank you for your answer, I did this: Successfully uninstalled Werkzeug-1.0.0 Successfully installed Werkzeug-0.16.0 but I still get exactly the same error that ends with ImportError: cannot import name 'secure_filename' – Santeau May 06 '20 at 23:00
  • 2
    Can u try to install this lib, see if it solves: `pip install Flask-Uploads` – Danizavtz May 06 '20 at 23:29
  • It was already installed (it printed Requirement already satisfied). – Santeau May 08 '20 at 05:39
  • 2
    Actually don't do this, or you will stick to a very old and outdated version of Werkzeug. Please see the other answers for a better solution. – Jürgen Gmach Jun 02 '22 at 06:41
22

You are using a broken version of Flask-Uploads.

Unfortunately, the maintainer of the package decided not to release a new version of the package to PyPi.

You can use Flask-Reuploaded as a drop-in replacement, which fixes your problem.

https://pypi.org/project/Flask-Reuploaded/

Jürgen Gmach
  • 5,366
  • 3
  • 20
  • 37
6

For Werkzeug version 2.1.2, just import secure_filename as:

from werkzeug.utils import secure_filename

Helder Daniel
  • 331
  • 3
  • 8
0

I ended up putting a

-e git://github.com/maxcountryman/flask-uploads.git#egg=elasticutils

in my requirements.txt file to get the latest version of flask-uploads from git.

0

Use flask-Reuploaded rather than flask-uploads it happens when you don't use werkzeug but still showing error so THE SOLUTION IS-- pip install Flask-Reuploaded