Importerror: cannot import name ‘cached_property’ from ‘werkzeug’

Have you come across the error importerror: cannot import name ‘cached_property’ from ‘werkzeug‘?

Well, in particular, this error could be hard if you are not familiar with werkzeug library.

So in this article, we will explain this error and provide a solution to fix it.

But before that, we will understand first the werkzeug library.

What is Werkzeug?

The Werkzeug is a comprehensive WSGI (Web Server Gateway Interface) utility library for Python.

Moreover, this can be used as a standalone module or as a dependency for web application frameworks like Flask.

Further, this library provides a collection of useful utilities and helpers for building web applications, such as URL routing, request and response handling, and error handling.

Now, why do we encounter this error?

What is importerror: cannot import name ‘cached_property’ from ‘werkzeug’?

The ImportError: cannot import name ‘cached_property’ from ‘werkzeug‘ is an error that occurs when the cached_property attribute cannot be imported from the werkzeug module.

In addition to this, it occurs for a variety of reasons, such as changes in the werkzeug module or conflicts with other installed packages.

Causes of the errorcannot import name ‘cached_property’ from ‘werkzeug’?

Besides from potential causes mentioned above, here are some more possible reasons which can guide you in troubleshooting the error.

Changes in the Werkzeug module

In version 1.0.0 of Werkzeug, the cached_property attribute was moved from the top-level werkzeug module to the werkzeug.utils module.

Obviously, if you are using a version of Werkzeug that is 1.0.0 or higher, you need to update your code.

To update your code to import the cached_property attribute from the correct location in Werkzeug version 1.0.0 or higher, you will need to change the import statement in your code.

If your code currently has a line that looks like this:

from werkzeug import cached_property

You will need to change it to this:

from werkzeug.utils import cached_property

This will ensure that the cached_property attribute is imported from the correct location in the Werkzeug module.

Conflicts with other installed packages

Meanwhile, when you have other packages installed that also use the Werkzeug module, there may be conflicts between the versions of Werkzeug required by the different packages.

In this case, you need to downgrade or upgrade one or more of the packages to resolve the conflict.

To check your Werkzeug version installed in your system use the pip command.

To do this open command prompt or terminal window and enter the following command:

pip show werkzeug

Alternatively, you can check the version of Werkzeug from within a Python script by importing the werkzeug module and accessing its version attribute.

Here’s an example:

import werkzeug
print(werkzeug.__version__)

Consequently, this will print the version number of Werkzeug to the console.

Incorrect installation

If the Werkzeug module was not installed correctly, it may not be able to find the cached_property attribute.

In this case, you can try uninstalling and reinstalling the Werkzeug module to see if that resolves the issue.

To uninstall and reinstall the Werkzeug module, you can use the pip command.

Open a command prompt or terminal window and enter the following commands:

pip uninstall werkzeug
pip install werkzeug

Specifically, the first command will uninstall the Werkzeug module from your system. Thus, the second command will install the latest version of the Werkzeug module.

How to fix the importerror: cannot import name ‘cached_property’ from ‘werkzeug’?

One solution to fix the ImportError: cannot import name ‘cached_property’ from ‘werkzeug’ error is to insert the following lines before importing Flask:

import werkzeug
werkzeug.cached_property = werkzeug.utils.cached_property

Another solution is to downgrade Werkzeug to version 0.16.0 temporarily.

This version of Werkzeug still has the cached_property attribute in the top-level werkzeug module.

To downgrade Werkzeug , you can use the following commands:

pip uninstall werkzeug
pip install werkzeug==0.16.0

Other Solutions

If none of the given solutions above works, try to consider the general solutions that include the following:

1. Check the Spelling

Make sure that you’re spelling the attribute name correctly. The ‘cached_property ‘ attribute should be spelled with an underscore (‘_’) between ‘cached ‘ and ‘property ‘.

2. Check the Module Name

Make sure that you’re importing the attribute from the correct module. The ‘cached_property ‘ attribute should be imported from the ‘werkzeug.utils’ module.

3. Check for Circular Import

Sometimes the error is caused by circular import in your code. Try to remove the circular import if possible.

Anyway here are the fixed errors that can help you in case you encounter these issues.

Conclusion

In conclusion, importerror: cannot import name ‘cached_property’ from ‘werkzeug’ implies thatyou’re using an outdated version of Werkzeug that doesn’t include the ‘cached_property’ attribute.

To fix this error upgrade the werkzeug library and update the code with the proper import statements or downgrade the library.

I think that’s all for this error. I hope this article has helped you fix the issue.

Until next time! 😊

Leave a Comment