Connect to Your WiFi using Python's wifi Module

Often we are unable to connect to WiFi or it’s not discoverable on our GUI. This usually happens on a fresh setup of Linux system that is unable to detect WiFi by itself.

While there are several alternatives to get it started, we will be using Python with its various functionalities to connect to or discover WiFi.

The library also comes with an executable that you can use to manage your WiFi connections. It supports various encryption types including WEP and WEP2.

Now that we know what we are working with, let us get started!

Getting Started

Firstly, we will have to install the "wifi" module since it does not come pre-packaged with Python. This can easily be done by using the pip package manager. To download and install the wifi module, use the following command −

pip install wifi

Now, if you don’t have access to the internet, you can always install it from a different computer and copy the file from your python folder to your linux system.

Once you have it installed, you can use its various features by importing it to your script by using the following command.

import wifi

However, in this tutorial, we will explore the wifi libraries command line interface. To launch this, type the below line in your terminal.

python -m wifi

Exploring the wifi Library in Python

Firstly, enter super user mode in your terminal. Once done, you are all set to use the command line interface.

python –m wifi

Now, if you want to scan for WiFi networks near your computer, use the scan command.

wifi scan

If you want to sort the data/output based on the strongest signal, you can use the sort keyword.

wifi scan | sort –rn

Now, if you want to connect to a WiFi network, use the following command −

wifi connect -a WIFI_NAME

Where WIFI_NAME is the name of the network you want to connect to.

Now, you will be requested to enter the password. Once done, you will be signed in and connected to the WiFi network.

The above method is for single time use, that is the WiFi configurations are not saved and will be forgotten upon sign out. In order to save your configuration, you need to use the add command.

wifi add nickname WIFI_NAME

where "nickname" is the name of your WiFi network.

Next time you want to easily connect to your WiFi, you can simply use the following command −

wifi connect nickname

If you want to know all the saved networks on your system, you can use the list command.

wifi list


You now know how to use the wifi library’s CLI to view, connect and save Wi-Fi networks. For more details on its various features or to explore methods to use it on a script rather than the command line interface, check out their official documentation at

Updated on: 04-Aug-2023


Kickstart Your Career

Get certified by completing the course

Get Started