Update media item using wordpress rest api in python

What you are trying to do is not something that endpoint is capable of. You can create attachments, you can even rotate and crop the file, but you cannot replace the attached file.

When cropping/scaling/rotating an image, a new attachment and ID are created for the newly edited image.

However, sending an UPDATE/PUT/POST request to an attachments endpoint with a new file will not replace it with that file.

Here is the relevant code: in WordPress in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php, the edit_media_item method:

https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php#L428-L676

How Do I Do It Then?

You don’t, and you shouldn’t.

Core provides no method for performing this action. There are plugins that allow you to replace an uploaded file but these are aimed at WP Admin, not the REST API, you will need to test and evaluate each on a per plugin basis, or build your own endpoint.

Note that replacing the contents of a file without changing its URL means browser cache and CDN caches won’t be updated cleanly, some CDNs may not support changing the contents at all ( e.g. Jetpacks Photon ), and Google/Search engines will be unaware.

If you need a single URL that always points to the latest version of a document, you should use PHP to handle that request and redirect to the actual file, or to serve that file. Replacing the file in the filesystem then relying on Nginx/Apache/etc to serve that file will give you problems.