title: Connecting to a wifi network using python
body: <p>I have this code that is supposed to connect to wifi using a given ESSID and password. Here is the code:</p>
  
  <pre><code>def wifi_connect(essid, password):
  # Connect to the wifi. Based on the example in the micropython
  # documentation.
  wlan = network.WLAN(network.STA_IF)
  wlan.active(True)
  if not wlan.isconnected():
      print('connecting to network ' + essid + '...')
      wlan.connect(essid, password)
      # connect() appears to be async - waiting for it to complete
      while not wlan.isconnected():
          print('waiting for connection...')
          print('checking connection...')
      print('Wifi connect successful, network config: %s' % repr(wlan.ifconfig()))
  else:
      # Note that connection info is stored in non-volatile memory. If
      # you are connected to the wrong network, do an explicity disconnect()
      # and then reconnect.
      print('Wifi already connected, network config: %s' % repr(wlan.ifconfig()))
  </code></pre>
  
  <p>At first, I got an error message that network was not installed. This was fixed by simply using pip to install network. After I ran this again, it told me that network has no attribute WLAN. How do I fix this? What am I doing wrong?</p>