You are not logged in.
Hello
I use Plasma X11, and I am currently trying to setup a custom command in Right Click > open with > Other > Use custom Command with "remember this app" toggled on, so I can reuse it on any file.
I would like to run a .py file inside a terminal immediately after double-clicking it so that I can see an output if there has to be any, just like on windows.
Doing my research, I found this command (edited & I use gnome-terminal as my default terminal for the moment) :
gnome-terminal -- bash -c 'python3; exec bash'Now, I need a way to get the full path + name of the file to automatically run it with python and I cannot find a way to do that. On windows, my notepad++ command to run python scripts looked like that :
cmd /k cd /d $(CURRENT_DIRECTORY) && "C:\Python39\python.exe" $(FULL_CURRENT_PATH)What I am looking for right now is something like $(FULL_CURRENT_PATH), but outside of an IDE.
I would prefer a method that can work for any type of file, because I want to use similar commands for other languages. Though, if you know about a more appropriate way to do this with python specifically, I'll take it too.
Any help would be very appreciated. Thanks in advance.
Last edited by Lajoie (2022-08-17 03:25:59)
Offline
So
pwddoesn't do what you want?
CLI Paste | How To Ask Questions
Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L
Offline
No pwd is not relevant - the OP needs a way to insert the name of the "file" icon (on the desktop presumably) that was clicked on for that action. This would be in KDE's documentation and would likely be a format string element like %f (which is a reasonable guess that you could test). Unfortunately, KDE documentation is not very well structured for this kind of thing. But I'd not surprise me if the format was the same as the Exec lines of desktop files.
However, it's almost certainly easier / better to just create and register desktop files of your own for these file types under ~/.share/applications. Why not use that approach?
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Thank you for your replies !
Trilby, as you suggested, %f does just what I need, and I am indeed trying to create a desktop entry under ~/.share/applications, but through plasma's GUI. I believe the "custom command" that plasma prompts me to input becomes the Exec line of a generated desktop entry. (I realized that when I checked the directory and recognized some of the entries that were listed in Right Click > open with > Other)
So, after I realized that, I found the documentation, did some testing, and wrote this command :
gnome-terminal -- bash -c 'FILE=%f; echo "$FILE"; python $FILE ; exec bash'It works well, but now I get "missing file" errors when I try to interact with other files using relative paths. Pure python fixes didn't work.
I think using cd right before running python would fix this issue, but the documentation reads :
A command line may contain at most one %f, %u, %F or %U field code.
That and also %d, the field code to get the parent directory's path, is Deprecated.
Is there an easy way to add a directory change to that Exec line ?
I'll see in the python settings if I can automatically change the directory to match the file's path.
Last edited by Lajoie (2022-08-15 17:47:22)
Offline
As you may guess, I'm not very familiar with desktop entries, but once you've got $FILE, can't you manipulate it as if you were at the command line?
cd $(dirname "$FILE")But I'm not sure this is a good approach.
CLI Paste | How To Ask Questions
Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L
Offline
It works well, but now I get "missing file" errors when I try to interact with other files using relative paths. Pure python fixes didn't work.
Please post the exact errors. This sounds like an issue with your python script(s) - in which case "pure pything" fixes would work.
But just the same, you seem to have missed the point about creating your own desktop files which would greatly streamline this whole process. Once you create your desktop files, you then can use Right Click > open with and select your desktop entry rather than using the other to enter a command line including a terminal.
While `cd $(dirname $FILE)` will likely work, it will only do so by temporarily brushing the problem(s) under the rug. You'd be far better off fixing things properly now.
Last edited by Trilby (2022-08-16 20:25:30)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
But just the same, you seem to have missed the point about creating your own desktop files which would greatly streamline this whole process. Once you create your desktop files, you then can use Right Click > open with and select your desktop entry rather than using the other to enter a command line including a terminal.
Maybe I've misunderstood or I'm missing something, but can executing a file and redirecting the output to a terminal count as (just) opening that file? That's why I'm not sure this is a good approach. Like using fins to string a bow or whatever.
CLI Paste | How To Ask Questions
Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L
Offline
... can executing a file and redirecting the output to a terminal count as (just) opening that file?
I don't know what this is in reference to. The OP wants "files" (icons in a desktop or filemanager really) to be handled based on their filetype which is a pretty common thing for desktop systems. Further, the OP wants python scripts (or the icons representing them) to respond to being (double-)clicked by opening a terminal and running the script in python in that terminal. Creating their own .desktop file for the given file type(s) would be the best way to acheive this.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Further, the OP wants python scripts (or the icons representing them) to respond to being (double-)clicked by opening a terminal and running the script in python in that terminal. Creating their own .desktop file for the given file type(s) would be the best way to acheive this.
Thanks. I didn't know 'open' could do that.
CLI Paste | How To Ask Questions
Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L
Offline
Hey ! So first off, cd $(dirname $FILE) works perfectly for me thank you ! I can't see why it wouldn't be a good approach though.
But just the same, you seem to have missed the point about creating your own desktop files which would greatly streamline this whole process. Once you create your desktop files, you then can use Right Click > open with and select your desktop entry rather than using the other to enter a command line including a terminal.
There is as small checkbox in the GUI that if you check it, it will keep the given command stored and then you could chose to use it as a default app to run any file from Right Click > open with. But I agree though, creating the whole desktop entry from scratch makes more sense after you learn how to do it.
Here are the errors I got.
Let's say I have a json file, data.json and a python script, main.py
both in the same "/home/Lajoie/Project" directory.
The command that outputs missing files errors is
gnome-terminal -- bash -c 'FILE=%f; echo "$FILE"; python $FILE ; exec bash'With it, running the main.py script containing :
from os import getcwd
print("The current directory is", getcwd())
with open("data.json", "r", encoding = "utf8") as f:
#anything
f.closewould output :
The current directory is /home/Lajoie/Desktop
Traceback (most recent call last):
File "/home/Lajoie/Project/main.py", line 24, in <module>
with open("data.json", "r", encoding = "utf8") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'data.json'The output would always be the same regardless of which directory I put the files in (Except /home/Lajoie/Desktop), or the type of file that I tried to open. But now it works all the time.
I don't know much technical stuff about the way python runs scripts but unless there is a specific python setting to automatically do the cd work for me, I think cd $(dirname $FILE) might be the best approach actually.
So anyway, here's my current working solution (a python.desktop file located under /usr/share/applications because that's the actual path to the desktop entries folder on my system):
[Desktop Entry]
Encoding=UTF-8
Name=Python
Type=Application
Terminal=true
Exec=bash -c 'FILE=%f ; cd $(dirname "$FILE") ; echo "$FILE" ; python $FILE ; exec bash'
Icon=/usr/share/icons/breeze-dark/apps/48/pythonbackend.svgI am satisfied with this solution but if you think there are better ways to achieve that, please tell me.
Again, thank you very much for your help.
Offline
So that error is just due to the python script. Assuming that data files will be in the current working directory is a very bad practice and will lead to failure in a wide range of situations. If you really want to open a file in the same directory as the script (which isn't a good design either, but is possible) you should use python's os.path.dirname(__file__), e.g.,
with open(os.path.dirname(__file__) + "/data.json", "r", encoding = "utf8") as f:Then you can get rid of the whole "bash -c ..." line from the desktop file:
Exec=python %f"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Then you can get rid of the whole "bash -c ..." line from the desktop file:
Exec=python %f
This would execute the script, but not in gnome-terminal.
CLI Paste | How To Ask Questions
Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L
Offline
It would run it in a terminal as the desktop file has Terminal=true. Note that if this were not the case, the "bash -c ..." line also wouldn't open in a terminal, it'd just run a needless shell.
Last edited by Trilby (2022-08-17 03:52:07)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
It would run it in a terminal as the desktop file has Terminal=true. Note that if this were not the case, the "bash -c ..." line also wouldn't open in a terminal, it'd just run a needless shell.
I finally get it. Thanks for your patience.
CLI Paste | How To Ask Questions
Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L
Offline
So that error is just due to the python script. Assuming that data files will be in the current working directory is a very bad practice and will lead to failure in a wide range of situations. If you really want to open a file in the same directory as the script (which isn't a good design either, but is possible) you should use python's os.path.dirname(__file__)
Yeah, it never really occurred to me but I guess that makes sense. I only use python for small personal projects so I will keep the most convenient way and leave the cd inside my command for now. I appreciate the advice though. Thanks !
Offline