Python SDK - Error creating a new wallet

Hi there,

I’m tring to create a wallet on my node that is catching up with the TestNet. There are no wallets yet, so I want to create the very first one using Python SDK.

I just followed this example: Create a wallet and generate an account on Alogrand Developer Docs.

When I run the example I get this error:

Traceback (most recent call last):
  File "/home/pi/Algorand_Python/wallet_create_example.py", line 10, in <module>
    wallet = Wallet("test_cusma", "test_cusma", kcl)
  File "/home/pi/.local/lib/python3.7/site-packages/algosdk/wallet.py", line 33, in __init__
    wallets = self.kcl.list_wallets()
  File "/home/pi/.local/lib/python3.7/site-packages/algosdk/kmd.py", line 87, in list_wallets
    return self.kmd_request("GET", req)["wallets"]
KeyError: 'wallets'

Inspecting the errors I fund that this Wallet’s method is failing:

    def list_wallets(self):
        """
        List all wallets hosted on node.

        Returns:
            dict[]: list of dictionaries containing wallet information
        """
        req = "/wallets"
        return self.kmd_request("GET", req)["wallets"]

I tried directly with the API and it seems working:

$ curl -H "X-KMD-API-Token:<my-test-net-token>" -i http://localhost:7833/v1/wallets

HTTP/1.1 200 OK
Content-Type: application/json
Date: Fri, 15 May 2020 18:08:52 GMT
Content-Length: 2

{}

Could this be due to the fact that there is no wallet yet on my node that is synchronizing with the TestNet?

You need to make sure the KMD process has started first before making the call in the Python, there is a little section up above the code section that talks about this - Creation methods - Algorand Developer Portal.

Try auto-starting KMD with goal and re-run. If it still fails, post the error here.

I manually stopped and re-started KMD using goal. The I runned once again the Python script getting the same error:

%Run new_account_in_wallet.py
Traceback (most recent call last):
  File "/home/pi/Documents/Algorand_Python/new_account_in_wallet.py", line 5, in <module>
    algo_wallet = wallet.Wallet("test_cusma", "test_cusma", kmd_client)
  File "/home/pi/.local/lib/python3.7/site-packages/algosdk/wallet.py", line 33, in __init__
    wallets = self.kcl.list_wallets()
  File "/home/pi/.local/lib/python3.7/site-packages/algosdk/kmd.py", line 87, in list_wallets
    return self.kmd_request("GET", req)["wallets"]
KeyError: 'wallets'

Short answer, it looks like the SDK expects a wallet to already exist, if you, like me, are using sandbox, there isn’t one and it errors.

To get around it, create an empty wallet. I’m looking into what the fix is to not get the error at all.

I see the issue.
When there is no wallet, the REST API returns:

{}

instead of what the Python SDK expects:

{
  "wallets": []
}