cannot import name 'secure_filename' from 'werkzeug'

て烟熏妆下的殇ゞ 提交于 2021-02-13 15:56:48

问题


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)

回答1:


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


来源:https://stackoverflow.com/questions/65853520/cannot-import-name-secure-filename-from-werkzeug

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!