Jeremy Thompson
Jeremy Thompson

Reputation: 65554

ModuleNotFoundError: No module named 'werkzeug.posixemulation'

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?

Upvotes: 6

Views: 16858

Answers (4)

Jeremy Thompson
Jeremy Thompson

Reputation: 65554

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   

Upvotes: 7

milahu
milahu

Reputation: 3549

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

Upvotes: 1

cv.samu.li
cv.samu.li

Reputation: 21

Works like this as well:

pip uninstall Werkzeug    
pip install Werkzeug==1.0.1

Upvotes: 2

BorjaEst
BorjaEst

Reputation: 756

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).

Upvotes: 1

Related Questions