LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2008, 09:05 PM   #1
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
Click to run Python in Konqueror?


I'm running KDE 3.5.9 on Kubuntu 8.04.

How do I associate .PY files to run in Python when I click them in Konqueror rather than opening in KWrite?
 
Old 05-06-2008, 05:32 AM   #2
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
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
 
Old 05-06-2008, 01:44 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Why not just make the files executable and have the first line as:
Code:
#!/usr/bin/env python
 
Old 05-06-2008, 10:20 PM   #4
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Original Poster
Rep: Reputation: 47
knudfl...

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.
 
Old 05-07-2008, 04:07 AM   #5
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
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'

Rgds
 
Old 05-07-2008, 05:42 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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).
 
Old 05-08-2008, 02:28 AM   #7
rainvoice
LQ Newbie
 
Registered: May 2008
Location: europe
Distribution: Ubuntu 7.10
Posts: 6

Rep: Reputation: 0
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.
 
Old 05-09-2008, 09:27 AM   #8
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Original Poster
Rep: Reputation: 47
Quote:
Originally Posted by knudfl View Post
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".

Thank you all for your help.
 
Old 05-12-2008, 07:39 PM   #9
rainvoice
LQ Newbie
 
Registered: May 2008
Location: europe
Distribution: Ubuntu 7.10
Posts: 6

Rep: Reputation: 0
Quote:
Originally Posted by ranger_nemo View Post
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".
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Konqueror right click menu cebo Mandriva 5 05-01-2007 06:46 AM
konqueror hangs on right click ayteebee Linux - Software 7 12-26-2005 04:56 PM
Konqueror click behavior azucaro Linux - Software 2 05-24-2005 11:57 AM
how to disable right-click in konqueror? crewler Linux - Newbie 2 07-12-2004 02:47 PM
Konqueror right click menu tenn_eric Linux - Software 0 08-04-2003 09:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration