'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?
Solution 1:[1]
Works like this as well:
pip uninstall Werkzeug
pip install Werkzeug==1.0.1
Solution 2:[2]
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).
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cv.samu.li |
| Solution 2 | BorjaEst |
| Solution 3 | Mila Nautikus |
