title: How to recover an Algorand Wallet with Python AlgoSDK?
body: <p>Although the official algosdk (Python SDK for Algorand) documentation suggests that a wallet can be recovered by simply invoking the following function (<a href="https://py-algorand-sdk.readthedocs.io/en/v1.2.1_a/kmd.html" rel="nofollow noreferrer">link</a>):</p>
  <pre><code>create_wallet(name, pswd, driver_name='sqlite', master_deriv_key=None)
  </code></pre>
  <p>with the fourth argument:</p>
  <pre><code>master_deriv_key (str, optional) – if recovering a wallet, include
  </code></pre>
  <p>wallet recovery does not work in my code and leads to experience an exception as well. Also the official Algorand documentation shows how to use the abovementioned function for recovering a wallet (<a href="https://developer.algorand.org/docs/features/accounts/create/#recover-wallet-and-regenerate-account" rel="nofollow noreferrer">link</a>):</p>
  <pre><code># recover the wallet by passing mdk when creating a wallet
  new_wallet = kcl.create_wallet(&quot;MyTestWallet2&quot;, &quot;testpassword&quot;, master_deriv_key=mdk)
  </code></pre>
  <p>Below, you can watch at my code, a very simple snippet that I coded to make some tests with Algorand SDK:</p>
  <pre><code>from algosdk import kmd
  from algosdk import mnemonic
  
  kmd_clt = kmd.KMDClient('855d39510cce40caf11de4c941b37632d1529ec970156214528a33a0ae8473b4', 'http://127.0.0.1:6969')
  if kmd_clt:
      kmd_wlt_mdk = None
      kmd_wlt_list = kmd_clt.list_wallets()
      for kmd_wlt in kmd_wlt_list:
          kmd_name = kmd_wlt['name']
          kmd_id = kmd_wlt['id']
          if kmd_name == 'wallet_name':
              kmd_wlt_hdl = kmd_clt.init_wallet_handle(kmd_id, 'wallet_password')
              if kmd_wlt_hdl:
                  kmd_wlt_mdk = kmd_clt.export_master_derivation_key(kmd_wlt_hdl, 'wallet_password')
              break
      if kmd_wlt_mdk:
          kmd_wlt = kmd_clt.create_wallet('wallet_name', 'wallet_password', master_deriv_key=kmd_wlt_mdk)
          kmd_wlt_hdl = kmd_clt.init_wallet_handle(kmd_wlt['id'], 'wallet_password')
          acc_addr_list = kmd_clt.list_keys(kmd_wlt_hdl)
          for acc_addr in acc_addr_list:
              account_address = acc_addr
              print(account_address)
              account_key = kmd_clt.export_key(kmd_wlt_hdl, 'wallet_password', account_address)
              print(account_key)
              account_mnemonic = mnemonic.from_private_key(account_key)
              print(account_mnemonic)
  </code></pre>
  <p>Below, you can watch at the Traceback and the error message returned at run-time:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/home/emiliano/anaconda3/lib/python3.7/site-packages/algosdk/kmd.py&quot;, line 63, in kmd_request
      resp = urlopen(req)
    File &quot;/home/emiliano/anaconda3/lib/python3.7/urllib/request.py&quot;, line 222, in urlopen
      return opener.open(url, data, timeout)
    File &quot;/home/emiliano/anaconda3/lib/python3.7/urllib/request.py&quot;, line 531, in open
      response = meth(req, response)
    File &quot;/home/emiliano/anaconda3/lib/python3.7/urllib/request.py&quot;, line 641, in http_response
      'http', request, response, code, msg, hdrs)
    File &quot;/home/emiliano/anaconda3/lib/python3.7/urllib/request.py&quot;, line 569, in error
      return self._call_chain(*args)
    File &quot;/home/emiliano/anaconda3/lib/python3.7/urllib/request.py&quot;, line 503, in _call_chain
      result = func(*args)
    File &quot;/home/emiliano/anaconda3/lib/python3.7/urllib/request.py&quot;, line 649, in http_error_default
      raise HTTPError(req.full_url, code, msg, hdrs, fp)
  urllib.error.HTTPError: HTTP Error 400: Bad Request
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;/home/emiliano/anaconda3/lib/python3.7/site-packages/algosdk/kmd.py&quot;, line 67, in kmd_request
      raise error.KMDHTTPError(json.loads(e)[&quot;message&quot;])
  algosdk.error.KMDHTTPError: wallet with same name already exists
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;algorand_test.py&quot;, line 49, in &lt;module&gt;
      kmd_wlt = kmd_clt.create_wallet('emiliano', 'emiliano', master_deriv_key=kmd_wlt_mdk)
    File &quot;/home/emiliano/anaconda3/lib/python3.7/site-packages/algosdk/kmd.py&quot;, line 118, in create_wallet
      return self.kmd_request(&quot;POST&quot;, req, data=query)[&quot;wallet&quot;]
    File &quot;/home/emiliano/anaconda3/lib/python3.7/site-packages/algosdk/kmd.py&quot;, line 69, in kmd_request
      raise error.KMDHTTPError(e)
  algosdk.error.KMDHTTPError: {
    &quot;error&quot;: true,
    &quot;message&quot;: &quot;wallet with same name already exists&quot;
  }
  </code></pre>
  <p>It seems to be clear how the <code>create_wallet</code> function is the sinner of such behavior which leads to get back the error &quot;wallet with same name already exists&quot;. Internals of Algorand SDK are very simple, APIs are wrappers for REST methods. Function <code>create_wallet</code> does simply (<a href="https://github.com/algorand/py-algorand-sdk/blob/develop/algosdk/kmd.py#L96" rel="nofollow noreferrer">link</a>):</p>
  <pre><code>def create_wallet(self, name, pswd, driver_name=&quot;sqlite&quot;,
                    master_deriv_key=None):
      &quot;&quot;&quot;
      Create a new wallet.
      Args:
          name (str): wallet name
          pswd (str): wallet password
          driver_name (str, optional): name of the driver
          master_deriv_key (str, optional): if recovering a wallet, include
      Returns:
          dict: dictionary containing wallet information
      &quot;&quot;&quot;
      req = &quot;/wallet&quot;
      query = {
          &quot;wallet_driver_name&quot;: driver_name,
          &quot;wallet_name&quot;: name,
          &quot;wallet_password&quot;: pswd
      }
      if master_deriv_key:
          query[&quot;master_derivation_key&quot;] = master_deriv_key
      return self.kmd_request(&quot;POST&quot;, req, data=query)[&quot;wallet&quot;]
  </code></pre>
  <p>I'm sure the master derivation key passed in input is correct since I've alredy checked it with the <code>goal</code> command from console.</p>
  <p>Has anyone else experienced this type of problem before?</p>