mojolang

Mojo Error: package 'base64' does not contain 'b64decode'


Using Mojo's online JupyterLab instance to run Mojo code, I am unable to decode a base64 value which I encoded using mojo itself. Here is my code

from base64

print(base64.b64encode("This is a "))
>>VGhpcyBpcyBhIA==

So far so good. Let's decode it back, now.

print(base64.b64decode("VGhpcyBpcyBhIA=="))

and here is the error message that I receive.

error: Expression [99]:20:17: package 'base64' does not contain 'b64decode'
    print(base64.b64decode("VGhpcyBpcyBhIA=="))
                ^

expression failed to parse (no further compiler diagnostics)

On their GitHub, I do see discussion which refers to the decoding function.


Solution

  • It seems like the b64decode not yet implemented at the moment (2023/sept). As you can see it's missing from the documentation also: https://docs.modular.com/mojo/stdlib/base64/base64.html. The mojo project still in early stage, some functionality missing but with time it will be implemented.

    In the mean time you could use python implementation instead, like this:

    import base64
    let x = base64.b64encode("This is a ")
    
    from python.python import Python
    let base64py = Python.import_module("base64")
    print(base64py.b64decode(x))