ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511
Rep:
Suggest : make a little script and place it in your path, e.g.
/usr/local/bin and add its name as a command to 'kmenuedit' to get an "icon"
Made this example 5 years ago:
Code:
#! /bin/sh
cd ~/games/PyTraffic-1.1/
exec python Game.py
You don't need the little shell script if you are going to make a menu entry. That's not what I'm trying to do... I'm trying to get it to run a .py program when I click it in file-browser-mode Konqueror, just like they run when I double-click them in Windows Explorer.
osor...
I have that, but Konqueror sees it as just a plain text file and opens it with the default editor. If I remove that association, then the file doesn't do anything when I click it. I'm trying to find an association that will run the .py program, not open it in the editor. I'm getting tired of hitting F4 to open a Konsole and typing in the ./whatever.py to run the program.
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511
Rep:
Well I can do that too (so far tested with 1 application)
I do: right click > choose: other application > write 'python' > save
And the next time the app just opens by clicking the script.py.
I did the test with a new version of 'PyTraffic' and the starting
script is still 'Game.py'
When I click a script whose mode is executable, the script runs. Perhaps the problem is that your script tries to make use of a terminal for input or output. The script is run without a controlling terminal, so you will have no feedback unless you use GUI code in your python script (of course if the script is non-interactive, any expected results such as file modifications should occur).
If the correct version of python is callable on commandline by typing "python" (usualy a symlink to the python-binary located in /usr/bin/python) and if python files are not associated to any default app in kde, the suggestions of osor should do the thing.
right click > choose: other application > write 'python' > save
And the next time the app just opens by clicking the script.py.
I had done that umpteen times and my programs didn't do anything... I have a handful of progs that reformat/tag the webcasts I download and listen to. The don't have any I/O, so they should have just quietly done their job, but didn't.
So, I went all the way back to a two-line "hello, world", and that worked. After some more basics, I discovered my problem was in the cwd... It isn't where the .PY file is. It defaults to your home. A simple os.chdir was all I needed.
A couple other things I discovered...
-- You don't need a #! line at the beginning of the program if you associate .PY with Python. It works with or without it.
-- You don't need to set the program as executable.
I guess it's working like you typed in "python prog.py".
-- You don't need a #! line at the beginning of the program if you associate .PY with Python. It works with or without it.
-- You don't need to set the program as executable.
I guess it's working like you typed in "python prog.py".
Congrats for your beneficial discoveries. All of this is correct and very useful in many cases and maybe in yours. But in most cases (e.g if you decide to make your scripts GPL-available for the comunity ) the preferable way to accomplish this is the #! line and making them executable for best X-platform compatibility. if you use a more complex structure with several separate modules it might be necessary to add the paths of your modules to the environment-var "PYTHONPATH". Have a deeper look to the os module, specially to "os.environ" and "os.path". That might give you further inspirations to improve your scripts.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.