All jobs

Fix "ImportError: cannot import name 'secure_filename' from 'werkzeug'" in a Flask-Uploads App

Yes. An agent traces the ImportError to Flask-Uploads' 2016 import line, swaps in Flask-Reuploaded, replaces the helper it dropped and exercises the upload routes.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

Werkzeug 1.0.0 stopped re-exporting its helpers from the top-level package, so from werkzeug import secure_filename, FileStorage fails on every Werkzeug released since February 2020. Flask-Uploads has exactly that line, at flask_uploads.py line 26, and its last release is 0.2.1 from July 2016, so the error is permanent: any Flask new enough to need Werkzeug 1.0 or later cannot import the extension. The traceback names Werkzeug, which sends people to the wrong package; the fix is on the Flask-Uploads side.

In an API like DiptoChakrabarty/flask-online-store, requirements.txt pins Flask-Uploads==0.2.1 beside Flask==1.1.2 and Werkzeug==0.16.1, the last Werkzeug that still had the top-level names, and the Dockerfile installs that file on python:3.8. The extension is used in three places. libs/image_uploader.py builds image_set = UploadSet("images", IMAGES) and wraps image_set.save(image, folder, name) and image_set.path(filename, folder); app.py imports configure_uploads and patch_request_class on line 4, sets UPLOADED_IMAGES_DEST on line 30, and calls patch_request_class(app, 20*1024*1024) and configure_uploads(app, image_set) on lines 53 and 54; resource/image.py catches UploadNotAllowed and serves files itself through send_file. Move Flask forward, or install on a machine that does not honour the Werkzeug pin, and the app stops at the import.

The agent reproduces both states in clean environments: the pinned set imports, and Flask-Uploads 0.2.1 with a current Flask and Werkzeug fails with the exact ImportError. Then it checks the maintained fork. Flask-Reuploaded keeps the flask_uploads import name and the same UploadSet, IMAGES, configure_uploads and UploadNotAllowed, imports secure_filename from werkzeug.utils, and its save and path methods take the same arguments, so libs/image_uploader.py and resource/image.py need no change. Its changelog lists two differences the agent checks against this code: patch_request_class was removed, with Flask's own MAX_CONTENT_LENGTH setting in its place, and serving uploaded files automatically is off unless UPLOADS_AUTOSERVE is set. The app serves images through its own route, so only the first applies.

So the pull request replaces one requirement line, deletes the import of patch_request_class, and turns the call on line 53 into app.config["MAX_CONTENT_LENGTH"] = 20 * 1024 * 1024. The agent then starts the app with the test client and sends an upload through the ImageUpload resource, a fetch through Images, and a request over the size limit, reading each response, and notes what it found on the way: a Flask that new also has no before_first_request, which app.py uses on line 56 to create tables, and pip refuses the pkg-resources==0.0.0 line that a system pip freeze left in the file. Both go in the pull request as separate items with their own fixes, so the reviewer can take them or not.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "This Flask app fails at import with ImportError: cannot import name 'secure_filename' from 'werkzeug'. In a clean virtual environment install the project as its requirements say and report whether it imports; then install it with a current Flask and Werkzeug, reproduce the error and quote the traceback with the file and line inside the extension that fails. Find every use of flask_uploads in the code: each imported name and each method called on an UploadSet. Install Flask-Reuploaded in place of Flask-Uploads and check each of those names and method signatures against the installed package, reading its changelog for what it removed or changed; replace patch_request_class with Flask's MAX_CONTENT_LENGTH at the same size, and say whether UPLOADS_AUTOSERVE matters for this app. Start the app through Flask's test client and exercise the upload route, the download route and a request over the size limit, quoting the status codes. List, separately, anything else that stops the app on the newer Flask, such as before_first_request, and any requirement line pip refuses, each with a proposed fix. Open a pull request with the dependency change, the code change, the traceback before and the test client output after.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request with three small changes: Flask-Uploads==0.2.1 becomes a pinned Flask-Reuploaded in requirements.txt, the patch_request_class import and call leave app.py, and MAX_CONTENT_LENGTH takes their place at the same twenty megabytes. The description opens with the traceback ending at flask_uploads.py line 26, then a table of every flask_uploads name the app uses and where the replacement provides it.

Below that is the test client transcript: the upload answering with the created filename, the download returning the file, and the oversized request refused with 413 by Flask itself rather than by the extension. The session's record keeps the two installs and every command, so a reviewer can see which Werkzeug each environment resolved.

The description ends with the findings outside this fix. For this app that is the before_first_request hook, which newer Flask releases do not have and which the agent proposes to replace with a create_all inside an app context at startup, and the pkg-resources==0.0.0 line that breaks installs outside the machine it was frozen on. A reviewer decides whether those land in the same pull request or the next one.

Run it on every image build that fails at import

This error arrives through a rebuild: a base image moves to a newer Python, a pin is loosened during a security upgrade, and the first request after deploy is a 500 because the app never imported. Add an automation to the Project with the prompt above and a webhook trigger, then add a step after the install in the build workflow that runs python -c "import app". When it fails and its output contains cannot import name 'secure_filename', a following step posts that output to the trigger's URL with Authorization: Bearer and the trigger's secret, kept as a repository secret. The posted text is placed after the saved prompt as the event.

Send an Idempotency-Key header to the trigger's URL set to the workflow run id, so a re-run of the failed job starts nothing new. Several services that share a base image tend to fail in the same hour; set the automation's max runs per hour with that in mind, since an event past the cap waits as a queued run until the hour has room.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

The ImportError names Werkzeug, and the first instinct is to pin Werkzeug back, which drags Flask back with it and blocks every other upgrade. Finding the real owner means reading the failing line inside an installed package, checking the fork's changelog for what it removed, and proving the swap against the routes that use it. The agent does that in its own sandbox with two environments side by side, and the session keeps each install with the versions it resolved.

Upload handling is also the part of an app nobody wants to test by hand: it needs a file, a token, a user folder and a request over the size limit. The run drives all four through the test client and brings back the status codes, so the pull request proves the extension swap rather than asserting it, and a team with several apps on the old extension starts one run per repository with the same request.

Frequently asked questions

Why does the traceback blame Werkzeug when Werkzeug is not my problem?

Because the failing import is inside Flask-Uploads, which asks Werkzeug for names it stopped exporting at the top level in 1.0.0. Werkzeug still has secure_filename, under werkzeug.utils. The extension was last released in 2016, so it will never update that line; the maintained fork already has.

Can I pin Werkzeug below 1.0 instead?

Only with a Flask old enough to accept it, since Flask 2.0 and later require Werkzeug 2 or newer. The pin keeps the app on a Flask line that no longer receives fixes. The agent shows the pinned environment working, then makes the change that lets the pins move.

What does Flask-Reuploaded change besides the import?

Two things its changelog names. patch_request_class is gone, because Flask's MAX_CONTENT_LENGTH has done that job since Flask 0.6, and uploaded files are not served automatically unless UPLOADS_AUTOSERVE is set. An app that serves its own files, as this one does through send_file, only needs the first change.

Will the agent change my upload folder or limits?

No. UPLOADED_IMAGES_DEST stays as configured, and the size limit is carried over at the same number of bytes. The test client request over that limit is there to show the limit still applies after the helper that set it is gone.

More jobs like this: Fix a bug from an alert

Keep reading