Score:3

notify-send giving syntax error with my custom Python file in 22.04

us flag

When I run a simple .py file with code

notify-send " ha ha "

I get notifications in 22.04 without any issues.

Then I tried the following code in another .py file. And it is giving a syntax error.

x = 1
count = x

while count <= 8:

    notify-send "Let's Take a Break!"
  
    
      
    sleep 60   

    count += 1 

    if count <= 8:        
        notify-send "Ok folks," "Let's get back to work!" 
    
        sleep 3600
else:
    notify-send "Ok folks," "Let's call it a day!" 

Please help me identify the issue. Thanks.

Score:4
jp flag

To be able to execute system commands like notify-send from within python scripts, you'll need to first import the os module then use it to execute that system command like so:

import os

os.system('notify-send "Let\'s Take a Break!"')

That being said, you might need to pay attention to other syntax issues like the sleep 60 call that might work in bash but python needs time.sleep(60) and you might want to first import the time module for that to work. Another thing is the indentation of if ... else, you need to pay attention to that too.

Therefore, the example code in your question should be:

import os
import time

x = 1
count = x

while count <= 8:
    os.system('notify-send "Let\'s Take a Break!"')
    time.sleep(60)
    count += 1

    if count <= 8:
        os.system('notify-send "Ok folks," "Let\'s get back to work!"')
        time.sleep(3600)
    else:
        os.system('notify-send "Ok folks," "Let\'s call it a day!"')

It is very important as well to correctly run your python script with python and not other interpreters like e.g. bash as I understand from this(which is most likely run by your shell e.g. bash and not the python interpreter):

When I run a simple .py file with code

notify-send " ha ha "

I get notifications in 22.04 without any issues.

user227495 avatar
us flag
Works great. As for the simple command, I just used " Allow this file to run as a program" in Properties and then right click " Run as a Program ". For the original code, I tried `python3 code.py` without success. Thanks.
user227495 avatar
us flag
Is it possible to play a sound file after the notify send? I tried using a line with mpv filepath/filename.mp3, but it said " NameError: name 'mpv' is not defined ". Thanks.
Raffa avatar
jp flag
@user227495 You might need to include the full path to the `mpv` executable … or alternatively https://towardsdatascience.com/personalized-your-python-notification-sounds-with-chime-d04e803d7615?gi=f8cb93f35369
user227495 avatar
us flag
Thanks, looks like some issue with pip : `pip install chime bash: /home/devel/.local/bin/pip: /snap/blender/2661/3.2/python/bin/python3.10: bad interpreter: No such file or directory`
Raffa avatar
jp flag
@user227495 You might need to first install `pip` if it's not installed with `sudo apt install python3-pip` ... also if mpv is installed on your system, it should work if run with the `os` module like so `os.system('mpv /path/to/file.mp3')` or any other program like VLC `os.system('vlc /path/to/file.mp3')`
user227495 avatar
us flag
Thanks, @Raffa , both issues are solved now. Worked exactly as you mentioned.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.