7

我正在尝试从 werkzeug.utils 导入secure_filename,但它会出错。它在我的基本虚拟环境下运行良好。

代码:

# 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

错误:

    (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)
4

2 回答 2

9

根据您自己的代码,该异常看起来像是Flask-Uploads在尝试from werkzeug import secure_filenamewhich should be 。from werkzeug.utils import secure_filename

通过Flask-Uploadsgithub 存储库,这似乎已在12 个月前修复

我会pip install -U flask-uploads在您的虚拟环境中尝试,以确保最新版本。

编辑

正如@mattficke 指出的那样,PyPi 版本已经过时,并且在 repo 上没有更新的版本。事实证明,您可以根据提交哈希直接安装,所以对于最新的(在撰写本文时):

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

或在requirements.txt

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

然后pip install -r requirements.txt

这很神奇:

>>> from flask_uploads import UploadSet,configure_uploads,IMAGES,DATA,ALL
>>> # No Exception
于 2021-01-22T22:30:11.207 回答
3

作为上述建议的解决方案的替代方案,您可以使用维护良好的 fork,称为Flask-Reuploaded.

您甚至不必更改 import 语句,因为它试图与不再维护良好的Flask-Uploads.

https://github.com/jugmac00/flask-reuploaded

此外,Flask-Reuploaded当然在 PyPI 上提供了最新的包:

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

于 2021-04-15T19:58:25.473 回答