Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

python - cannot import name 'secure_filename' from 'werkzeug'

I'm trying to import secure_filename from werkzeug.utils and it shoot an error. It works fine under my base virtual env.

code:

# Flask packages
from flask import Flask, render_template, request, session, redirect, flash, send_file
from flask_bootstrap import Bootstrap 
from flask_uploads import UploadSet,configure_uploads,IMAGES,DATA,ALL

# Systems
import os 
import sys
import json
from werkzeug.utils import secure_filename

Error:

    (absa_annotation) C02QM3FSFVH3:ABSA-annotation-tool kwunkeilau$ python3 app.py 
Traceback (most recent call last):
  File "app.py", line 4, in <module>
    from flask_uploads import UploadSet,configure_uploads,IMAGES,DATA,ALL
  File "/Users/kwunkeilau/anaconda3/envs/absa_annotation/lib/python3.7/site-packages/flask_uploads.py", line 26, in <module>
    from werkzeug import secure_filename, FileStorage
ImportError: cannot import name 'secure_filename' from 'werkzeug' (/Users/kwunkeilau/anaconda3/envs/absa_annotation/lib/python3.7/site-packages/werkzeug/__init__.py)
question from:https://stackoverflow.com/questions/65949820/numerous-errors-when-deploying-flask-app-on-a-linode-server

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That exception looks like Flask-Uploads is trying to from werkzeug import secure_filename which should be from werkzeug.utils import secure_filename, as per your own code.

Going by the Flask-Uploads github repo this appears to have been fixed 12 months ago.

I'd try pip install -U flask-uploads in your virtual environment, to ensure the latest version.

EDIT:

As @mattficke points out, the PyPi version is dated, and there's not a more recent release on the repo. Turns out you can install directly based on a commit hash, so for the latest (at the time of writing this):

pip install git+https://github.com/maxcountryman/flask-uploads.git@f66d7dc

Or in a requirements.txt:

git+https://github.com/maxcountryman/flask-uploads.git@f66d7dc

Then pip install -r requirements.txt.

Which works wonders:

>>> from flask_uploads import UploadSet,configure_uploads,IMAGES,DATA,ALL
>>> # No Exception

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...