5

Hitting this error when deploying a Python Flask website:

ModuleNotFoundError: No module named 'werkzeug.posixemulation'

I found this Chinese link that says to install werkzeug.

So I did a pip install werkzeug locally and then a pip freeze and it says the version was Werkzeug==2.0.1

I added Werkzeug==2.0.1 to the requirements, however I still get the error when spinning up the Flask website.

The issue has been reported: https://github.com/pallets/secure-cookie/issues/12

How can I fix this?

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321

4 Answers4

7

Check the binary repository for the previous version. In our case it was 1.0.1

Solution

Add Werkzeug Module to the Requirements.txt file explicitly specifying the previous version 1.0.1, eg:

Werkzeug==1.0.1

If your Python Flask website deployment is not automated (like mine with Infrastructure As Code) then you'll need to uninstall first:

pip uninstall Werkzeug   
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
2

Works like this as well:

pip uninstall Werkzeug    
pip install Werkzeug==1.0.1
cv.samu.li
  • 21
  • 2
1

Probably one of your flask extensions is requiring an old version of Wekzeug. Fist I would check where it is failing, for example:

Traceback (most recent call last):
  ...
  File "/home/.../lib/python3.9/site-packages/flask_caching/.../filesystem.py", line 7, in <module>
    from werkzeug.posixemulation import rename
ModuleNotFoundError: No module named 'werkzeug.posixemulation'

Here I am using an old version of "flask_caching" where flask 2.0.x was not upgraded. In this case the solution is to upgrade "flask_caching" to the last version (which uses flask 2.0.x).

Your specific case with secure-cookie (got it from the issue link). If your extension did not upgrade to use flask 2.0.x or depends on an old version of Werkzeug you need to downgrade flask to "~=1.1.0" as flask 2.0.x bumps minimum versions of other Pallets projects (including Werkzeug >= 2).

BorjaEst
  • 390
  • 2
  • 11
1

werkzeug.posixemulation was removed in #1759

from werkzeug.posixemulation import rename

werkzeug.posixemulation.rename was used to get a "unix rename" on windows

since python 3.3, "unix rename" is provided by os.replace

→ duplicate of Is os.replace() atomic on Windows?

drop-in replacement:

from os import replace as rename
milahu
  • 2,447
  • 1
  • 18
  • 25