Adding a script as a KDE Menu item "Could not find the program"

My python project uses PySimpleGUI, which I had to install in a venv inside the project folder. I made a script file in the project folder that handles this OK, as follows;

#! /bin/bash
source venv/bin/activate
python Main.py
deactivate

This script works great by itself.
It’s when I add it as an item to the KDE menu that there’s a problem. Trying to run this script from the KDE menu causes the following error:
image

Here’s what the menu entry looks like, very simple, just a path to the script, with quotes around it for the spaces in the path - I even used the little file browser button next to the “Program” field to choose the file, so it populated the field itself - the file path is correct, I don’t know what it’s problem is.

Just a thought as I have no idea if it isn’t this but have you set the script to be executble?

And you should FULLY qualify everything (including the source statement).

Good thought but I already checked - the file is on an ntfs drive which seems to allow it to be executable by default, but I thoroughly tested this concept earlier - by pointing the path to a script that resides on an ext drive, in the home folder, so super short path with no spaces, and ext partition - got exactly the same error.

What’s infuriating is I do have KDE Menu items pointing to other .sh files that work fine, even on ntfs partition…

1 Like

What do you mean?

Absolute pathnames. Full qualified directory structure.

/home/domarius/My_Files/Projects/Invoicer/work/Invoicer/Invoicer.sh
This is the Program line: no quotes, no space (I failed to escape space with backslash)
I changed a bit:

cd /home/domarius/My_Files/Projects/Invoicer/work/Invoicer
python -m venv .Invoicer
chmod +x .Invoicer/bin/activate

when you manually enter your environment:

source .Invoicer/bin/activate
pip install PySimpleGUI

this is the Invoicer.sh

#! /bin/bash
source /home/domarius/My_Files/Projects/Invoicer/work/Invoicer/.Invoicer/bin/activate
python /home/domarius/My_Files/Projects/Invoicer/work/Invoicer/Main.py
deactivate
exit
deactivate

paste into your ~/.bashrc

deactivate() {
    # reset old environment variables
    if [ -n "$_OLD_VIRTUAL_PATH" ] ; then
        PATH="$_OLD_VIRTUAL_PATH"
        export PATH
        unset _OLD_VIRTUAL_PATH
    fi
    if [ -n "$_OLD_VIRTUAL_PYTHONHOME" ] ; then
        PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
        export PYTHONHOME
        unset _OLD_VIRTUAL_PYTHONHOME
    fi

    # This should detect bash and zsh, which have a hash command that must
    # be called to get it to forget past commands.  Without forgetting
    # past commands the $PATH changes we made may not be respected
    if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
        hash -r
    fi

    if [ -n "$_OLD_VIRTUAL_PS1" ] ; then
        PS1="$_OLD_VIRTUAL_PS1"
        export PS1
        unset _OLD_VIRTUAL_PS1
    fi

    unset VIRTUAL_ENV
    if [ ! "$1" = "nondestructive" ] ; then
        # Self destruct!
        unset -f deactivate
    fi
}

That’s what I thought you meant - but I thought from my screenshot it’s obvious I’ve done that. Am I missing something?

Well it’s not the path that the issue - I tried making a shortcut to a script in my home folder, this is an ext partition, and this is a very short path, with no spaces: /home/domarius/light.sh, and I got the same error.

But I do have working KDE menu items pointing to scripts (even in long paths, with spaces, on ntfs partitions), it’s almost as if my existing examples work, but any new examples I make from today will not work. I have been getting updates from EndeavourOS, maybe something has changed?

Double check pls: Try Program without quotation marks.


If I use quotation marks I get the same error. Otherwise it works.

I inserted this into Main.py
And as some elements are new to me I started from here

3 Likes

Was actually something I thought of than thought and didn’t comment that it would have been tried :joy:, but yer a lot of programs hate those quotations

Thanks eso, the various info in your post actually got me to the solution… I want to mark your post as the solution since it technically contains the answers, but I’m summing everything up here plus some other details, and people can read this post and see that I credit you with the info I needed to solve the problem.

The 3 major problems were;

  • KDE Menu doesn’t like quotes, but puts them in automatically if you use the button to browse to your file and the path contains spaces
  • The path contains spaces
  • The script needs the proper header

I thought Linux bash scripting and launching shortcuts was pretty much the same as Windows here, but it seems I’ve managed to escape the gory details until now, hence my utter confusion…

So - I realised a couple things that were obfuscating the truth and doing a good job of keeping me in the dark… first thing; I realised the only good example I have of a script being run by the KDE Menu is a script I didn’t write, it’s for 4kdownloader and the standalone version comes with a script you can run that just runs it in place. Opening it up, it seems it does some fancy stuff to run in place, it builds an absolute path to the executable (and also sets up some environment variables)

#!/bin/sh

SCRIPT_DIR=$(dirname "$0")
SCRIPT_DIR=`cd "$SCRIPT_DIR"; pwd` #make path absolute

LD_LIBRARY_PATH=$SCRIPT_DIR
export LD_LIBRARY_PATH

"$SCRIPT_DIR"/4kvideodownloader-bin $*

SECONDLY - the path to that program, just so happens to not contain spaces! I think I purposefully set it up that way due to previous issues and seem to have forgotten that’s why I did it. OR I just got extremely lucky when I made this folder. It’s on an ntfs partition, and instead of “Program Files” it’s “Programs-Linux” /mnt/data/Programs-Linux/4kvideodownloader/

You’d think then, that KDE Menu would NOT insert those quotes itself when I use it’s own “browse” button??


THAT button right there, using that button to browse to your file, if that resulting path contains spaces, quotes will be inserted around the path! Which makes sense! Except that it seems KDE Menu doesn’t like quotes! Why would it do that to itself? Anyway, moving on…

Ok so I copy my invoicer.sh script to my home folder (so it doesn’t have spaces in the path) and edit the script to cd into the project folder before starting the venv and running python. I make a shortcut to that in the KDE Menu. New error: execvp: Exec format error
Searching the net for that shows me that the header of the bash file is wrong.

So the final script that works when I link it in a KDE Menu item, is…

#!/bin/bash
cd "/home/domarius/My Files/Projects/Invoicer/work/Invoicer/"
source venv/bin/activate
python Main.py
deactivate
3 Likes

I know but who knows how these peoples minds work, all I know is all up they are a lot smarter than me. My guess is for security to make copy and pasting things eg harder to link to the actual program/process (take this with a grain of salt, its just my opinion with no real research)

My bet is because the Linux community does not like to use spaces in their paths, so although support for paths with spaces may be built in to Linux software in various ways, some of the smaller cases like this never get found because no one uses spaces anyway. This is another way I was led away from the truth - those quotes were there, that implies it supports spaces in the path. But the reality is, you shouldn’t do it.

1 Like

Although you have a working solution, I was just wondering about the quotes. You used single quotes in the first picture. Would that work with double quotes?

Like from

'some text'

to

"another text"

I’ve seen somewhat related problems with certain other apps.

Also, some apps seem to require both double and single quotes for some reason, like

"'yet another text'"

So there are different implementations for handling input…

2 Likes

I think this all depends on the application/script that is being run.

Yep, I did try both, specifically because I’m used to double quotes and I thought it was interesting that KDE Menu automatically added single quotes instead. So naturally my next choice was to try the double quotes - same problem.

Though, no I did not try "'both single and double'", I’ve never seen that so never thought to try it. I’m not at home but I could give that a go later.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.