LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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
 
Thread Tools Search this Thread
Old 05-05-2008, 10:05 PM   #1
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142
Thanked: 1
Click to run Python in Konqueror?


[Log in to get rid of this advertisement]
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?
ranger_nemo is offline     Reply With Quote
Old 05-06-2008, 06:32 AM   #2
knudfl
Senior Member
 
Registered: Jan 2008
Location: Copenhagen, Denmark
Distribution: pclos2009.2, slack13, Debian5Lenny +30+ others, for test only.
Posts: 3,685
Thanked: 421
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
knudfl is offline     Reply With Quote
Old 05-06-2008, 02:44 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Thanked: 26
Why not just make the files executable and have the first line as:
Code:
#!/usr/bin/env python
osor is offline     Reply With Quote
Old 05-06-2008, 11:20 PM   #4
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142
Thanked: 1

Original Poster
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.
ranger_nemo is offline     Reply With Quote
Old 05-07-2008, 05:07 AM   #5
knudfl
Senior Member
 
Registered: Jan 2008
Location: Copenhagen, Denmark
Distribution: pclos2009.2, slack13, Debian5Lenny +30+ others, for test only.
Posts: 3,685
Thanked: 421
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
knudfl is offline     Reply With Quote
Old 05-07-2008, 06:42 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Thanked: 26
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).
osor is offline     Reply With Quote
Old 05-08-2008, 03:28 AM   #7
rainvoice
LQ Newbie
 
Registered: May 2008
Location: europe
Distribution: Ubuntu 7.10
Posts: 6
Thanked: 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.
rainvoice is offline     Reply With Quote
Old 05-09-2008, 10:27 AM   #8
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142
Thanked: 1

Original Poster
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.
ranger_nemo is offline     Reply With Quote
Old 05-12-2008, 08:39 PM   #9
rainvoice
LQ Newbie
 
Registered: May 2008
Location: europe
Distribution: Ubuntu 7.10
Posts: 6
Thanked: 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.
rainvoice is offline     Reply With Quote

Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 07:46 AM
konqueror hangs on right click ayteebee Linux - Software 7 12-26-2005 05:56 PM
Konqueror click behavior azucaro Linux - Software 2 05-24-2005 12:57 PM
how to disable right-click in konqueror? crewler Linux - Newbie 2 07-12-2004 03:47 PM
Konqueror right click menu tenn_eric Linux - Software 0 08-04-2003 10:45 PM


All times are GMT -5. The time now is 05:10 PM.

Main Menu
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.
Advertisement
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Click Here to receive a complimentary subscription courtesy of LQ.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration