Getting AttributeError: module 'base64' has no attribute 'decodestring' error while running on python 3.9.6

35k Views Asked by At

Issue description:

Getting AttributeError: module 'base64' has no attribute 'decodestring' error while running on python 3.9.6

Steps to reproduce:

Below is a dummy program, while running on python 3.9.6, I am getting `AttributeError: module 'base64' has no attribute 'decodestring'`` error:

from ldif3 import LDIFParser

parser = LDIFParser(open('dse3.ldif', 'rb'))
for dn, entry in parser.parse():
    if dn == "cn=Schema Compatibility,cn=plugins,cn=config":
        if entry['nsslapd-pluginEnabled'] == ['on']:
            print('Entry record: %s' % dn)

Error message:

python3.9 1.py                              ✔    venvpy3.9   11:12:01 
Traceback (most recent call last):
  File "/Users/rasrivas/local_test/1.py", line 4, in <module>
    for dn, entry in parser.parse():
  File "/Users/rasrivas/local_test/venvpy3.9/lib/python3.9/site-packages/ldif3.py", line 384, in parse
    yield self._parse_entry_record(block)
  File "/Users/rasrivas/local_test/venvpy3.9/lib/python3.9/site-packages/ldif3.py", line 357, in _parse_entry_record
    attr_type, attr_value = self._parse_attr(line)
  File "/Users/rasrivas/local_test/venvpy3.9/lib/python3.9/site-packages/ldif3.py", line 315, in _parse_attr
    attr_value = base64.decodestring(line[colon_pos + 2:])
AttributeError: module 'base64' has no attribute 'decodestring'

python version

python --version 
Python 3.9.6

Operating system:

macOS 11.5.2

Python version:

python --version 
Python 3.9.6

python-ldap version:

ldif3-3.2.2

4

There are 4 best solutions below

1
BillyBBone On BEST ANSWER

From the docs for Python 3.8, base64.decodestring() is described as a:

Deprecated alias of decodebytes().

It looks like the base64.decodestring() function has been deprecated since Python 3.1, and removed in Python 3.9. You will want to use the bas64.decodebytes() function instead.

1
George D'Paula On

Try install base64 in your python, use the command:

pip install pybase64

or:

pip3 install pybase64
0
Alex On

This is an old post but I am leaving here in case someone stumbles upon the same question.

To decode base64 to bytes use base64.b64decode()

You can find more info here

0
Bishal Ghimire On

decodestring() no longer exists from v 3.9. Checkout this docs, use base64.decodebytes() instead