I wrote a python code for getting random text into a .txt file. Now I want to send this random text into notification area via 'notify-send' command. How do we do that?

7 Answers 7


We can always call notify-send as a subprocess, e.g like that:

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import subprocess

def sendmessage(message):
    subprocess.Popen(['notify-send', message])

Alternatively we could also install python-notify2 or python3-notify2 and call the notification through that:

import notify2

def sendmessage(title, message):
    notice = notify2.Notification(title, message)
  • 2
    I had to use subprocess.Popen(['notify-send', message]) to make the first example work.
    – lgarzo
    Commented Feb 29, 2012 at 16:52
  • The second one a pynotify.init("Test") and pynotify.Notification(title, message).show(). By the way I'm „Learning Python The Hard Way”, so I might just overlook something...
    – lgarzo
    Commented Feb 29, 2012 at 17:07
  • So sorry for that. I was unable to test this earlier on my box at work - my bad. Edited the missing bits in.
    – Takkat
    Commented Feb 29, 2012 at 17:21
  • There is a notify2 package in Python 2.7.12 as well so might as well use it on Ubuntu 16.04 as well 18.04 and 20.04 so you don't have to modify your code. Commented Oct 31, 2020 at 19:13
  • 1
    @WinEunuuchs2Unix: thanks for the notice - included :)
    – Takkat
    Commented Oct 31, 2020 at 20:02


Whilst you can call notify-send via os.system or subprocess it is arguably more consistent with GTK3 based programming to use the Notify gobject-introspection class.

A small example will show this in action:

from gi.repository import GObject
from gi.repository import Notify

class MyClass(GObject.Object):
    def __init__(self):

        super(MyClass, self).__init__()
        # lets initialise with the application name

    def send_notification(self, title, text, file_path_to_icon=""):

        n = Notify.Notification.new(title, text, file_path_to_icon)

my = MyClass()
my.send_notification("this is a title", "this is some text")
  • Not just more consistent with Gtk programming, but also it's a single process: there's no fork-exec going on, so no waste of resources on spawning new process just to send a notification bubble, plus Popen() will call shell to run the command, so there's shell process popping up too. Commented Oct 15, 2018 at 23:20
  • @SergiyKolodyazhnyy Using Gtk when you don't need to requires a lot of work. Much simpler to install python-notify2 or python3-notify2. As for Popen() the newer subprocess methods (using popen) allow you to turn off the shell which is a security risk at times. I probably could have worded this better but I'm still learning python. Commented Oct 31, 2020 at 19:16
  • 1
    From the link you've posted, you can also find Gio.Notification. I think they are separate? When would you use one or the other?
    – phil294
    Commented Jul 17, 2023 at 21:20

To answer Mehul Mohan question as well as propose the shortest way to push a notification with title and message sections:

import os
os.system('notify-send "TITLE" "MESSAGE"')

Putting this in function might be a bit confusing due to quotes in quotes

import os
def message(title, message):
  os.system('notify-send "'+title+'" "'+message+'"')
  • 4
    You could suggest an edit to that post.
    – muru
    Commented Oct 26, 2015 at 10:25
  • 3
    What about using 'notify-send "{}" "{}"'.format(title, message) rather than adding strings?
    – Stam Kaly
    Commented Jan 31, 2017 at 21:22

You should use notify2 package, it is a replacement for python-notify. Use it as followed.

pip install notify2

And the code:

import notify2
notify2.init('app name')
n = notify2.Notification('title', 'message')
import os
os.system('notify-send '+mstr)
  • How do I customize this notification? Like heading, image in notification, etc.?
    – mehulmpt
    Commented Mar 25, 2015 at 18:34
  • 3
    This version is vulnerable to arbitrary command execution.
    – Steve
    Commented Oct 16, 2018 at 3:05
  • If my edit is approved, I fixed the security problem Commented Oct 14, 2020 at 7:37

For anyone looking at this in +2018, I can recommend the notify2 package.

This is a pure-python replacement for notify-python, using python-dbus to communicate with the notifications server directly. It’s compatible with Python 2 and 3, and its callbacks can work with Gtk 3 or Qt 4 applications.


PyNotify2, suggested by many answers, considers itself as deprecated as of late 2020:

notify2 is - or was - a package to display desktop notifications on Linux. Those are the little bubbles which tell a user about e.g. new emails.

notify2 is deprecated. Here are some alternatives:

  • desktop_notify is a newer module doing essentially the same thing.
  • If you’re writing a GTK application, you may want to use GNotification (intro, Python API).
  • For simple cases, you can run notify-send as a subprocess. The py-notifier package provides a simple Python API around this, and can also display notifications on Windows.

So, given the above suggestions:

  • The notify-send subprocess approach is already explained in other answers, and py-notifier can simplify that, with an added bonus of working on Windows platforms using win10toast, but also with all the drawbacks of a subprocess call under the hood:
from pynotifier import Notification

    title='Notification Title',
    description='Notification Description',
  • desktop_notify seems to use DBus directly, just like PyNotify2, and has dbus-next as its sole dependency.
notify = desktop_notify.aio.Notify('summary', 'body')
await notify.show()
  • fossfreedom's answer covers GTK's gi introspection route. But please note he uses a different API than the one mentioned above:
    • There's the Gio.Notification API, from Gio 2.4 onwards, mentioned by pynotify2
    • And there's the Notify API, from GLib 2.0 onwards, used in @fossfreedom's code snippet.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .