[FIX] Attribute Error : ‘str’ object has no attribute ‘decode’ in Python 3

The error, ‘str’ object has no attribute ‘decode’ occurs in Python 3 when you try to decode Unicode characters in a string.

If you have upgraded from Python 2 to Python 3, you will notice that it worked in Python 2. But that’s not working in Python 3.

Firstly, let’s see what is causing this error.

What causes the Attribute Error: ‘str’ object has no attribute ‘decode’?

Encoding means converting a string object to bytes. And decoding means converting bytes to a string.

encode and decode

 

The default encoding standard in Python 2 is ASCII. Meaning, if your program is expecting ASCII characters, they do not need to be decoded. However, if your program is expecting non-ASCII characters, those characters have to be decoded and then used. Thus, the decode function is used to decode the contents from, say, UTF-8 to ASCII format.

In Python 3, the default encoding standard is UTF-8. Refer to the Python Official site for more details. It means that the characters in UTF-8 format are understood in Python 3 as is. So, there is no need to use the decode function.

Also, in certain cases when you are using external packages in your program, like h5pyPyJWT, and Flask-JWT, the updates in these libraries might lead to this error.

Now that we’ve understood what is causing the error, let’s discuss various ways of fixing this error.

Fix 1: Don’t decode UTF-8 characters in Python 3

It is not necessary to use the decode function on a string that has Unicode characters in Python 3. By doing so, you are trying to decode an already decoded object.

If you are not sure of the version of Python you are using, check this link.

If you have something like this,

data = content[0][1].decode('utf-8')

Try using :

data = content[0][1]

Fix 2: Use the right package version

If you see this error after upgrading your external libraries, consider freezing the version of your external libraries that works for you. Follow the below steps to do so.

Method 1: Using the requirements.txt

1. Open the Command Prompt.

2. Enter the below commands to view the current version of the packages installed on the system.

pip freeze > requirements.txt
type requirements.txt

 

3. To freeze the version, enter the package name and the version name as shown below.

package_name==version_number > requirements.txt

 

For example, To freeze the PyJWT package to 1.7.1, enter the below command.

PyJWT==1.7.1 > requirements.txt

 

4. Now, install the required version of the package with the below command.

pip install -r requirements.txt

 

solving str has no attribute decode

Method 2: Using the force reinstall flags

You can also below the command to install the required version of the package.

pip install <package_name>==<version_number> –force-reinstall

 

For example, to install the Flast-JWT-Extended package version 4.1.0,  the command is

pip install Flask-JWT-Extended==4.1.0 -force-reinstall

 

Hope this helps.

Conclusion

In this article, we have discussed what causes the Attribute error: ‘str’ object has no attribute ‘decode’ in Python 3 and also discussed different ways of fixing the same.

If you found this article helpful, kindly comment and let us know.

If you enjoyed reading, share this article.

Anusha Pai is a Software Engineer having a long experience in the IT industry and having a passion to write. She has a keen interest in writing Python Errorfixes, Solutions, and Tutorials.

3 thoughts on “[FIX] Attribute Error : ‘str’ object has no attribute ‘decode’ in Python 3”

Leave a Comment