> ## Content Index
> Fetch the complete content index at: https://www.bagz.fr/llms.txt
> Use this file to discover other available public pages before exploring further.

# Create an interactive command-line menu using Python
- URL: https://www.bagz.fr/create-an-interactive-command-line-menu-using-python/
- Published: 2015-01-12T10:43:11.000Z
- Updated: 2015-01-12T10:43:11.000Z
- Author: bgazui
- Tags: Linux, Python, #Migrated-1790668826633, #wp, #wp-post, #Import 2026-09-29 08:00

It can be necessary to create a simple interactive menu on CLI (Command-Line Interface) using Python to allow users to make some choices while executing a script/program. As there is no standard library for this in Python, you will have to adapt it by yourself.

Here is a very simple example of how to do this. Feel free to modify, update and improve it as depending on your needs and your expectations ;) !

#!/usr/bin/env python  
\# -\*- coding: utf-8 -\*-  
#title :menu.py  
#description :This program displays an interactive menu on CLI  
#author :  
#date :  
#version :0.1  
#usage :python menu.py  
#notes :  
#python\_version :2.7.6   
#=======================================================================  
  
\# Import the modules needed to run the script.  
import sys, os  
  
\# Main definition - constants  
menu\_actions = {}   
  
\# =======================  
\# MENUS FUNCTIONS  
\# =======================  
  
\# Main menu  
def main\_menu():  
 os.system('clear')  
  
 print "Welcome,\\n"  
 print "Please choose the menu you want to start:"  
 print "1\. Menu 1"  
 print "2\. Menu 2"  
 print "\\n0\. Quit"  
 choice = raw\_input(" >> ")  
 exec\_menu(choice)  
  
 return  
  
\# Execute menu  
def exec\_menu(choice):  
 os.system('clear')  
 ch = choice.lower()  
 if ch == '':  
 menu\_actions\['main\_menu'\]()  
 else:  
 try:  
 menu\_actions\[ch\]()  
 except KeyError:  
 print "Invalid selection, please try again.\\n"  
 menu\_actions\['main\_menu'\]()  
 return  
  
\# Menu 1  
def menu1():  
 print "Hello Menu 1 !\\n"  
 print "9\. Back"  
 print "0\. Quit"  
 choice = raw\_input(" >> ")  
 exec\_menu(choice)  
 return  
  
  
\# Menu 2  
def menu2():  
 print "Hello Menu 2 !\\n"  
 print "9\. Back"  
 print "0\. Quit"   
 choice = raw\_input(" >> ")  
 exec\_menu(choice)  
 return  
  
\# Back to main menu  
def back():  
 menu\_actions\['main\_menu'\]()  
  
\# Exit program  
def exit():  
 sys.exit()  
  
\# =======================  
\# MENUS DEFINITIONS  
\# =======================  
  
\# Menu definition  
menu\_actions = {  
 'main\_menu': main\_menu,  
 '1': menu1,  
 '2': menu2,  
 '9': back,  
 '0': exit,  
}  
  
\# =======================  
\# MAIN PROGRAM  
\# =======================  
  
\# Main Program  
if \_\_name\_\_ == "\_\_main\_\_":  
 \# Launch main menu  
 main\_menu()