[Tutor] executing with double click on linux

Jonathon Sisson sisson.j at gmail.com
Sun Oct 15 01:28:57 CEST 2006


I'm afraid I don't have enough experience with Gnome to answer your 
questions, Alfonso.  I will share what I know, though.  If you could, 
run this:

$ gnomevfs-info <yourfile>.pyc | grep MIME

and see what it says?  (If it gives you nothing back, then you need to 
register a MIME type for .pyc files.

http://www.gnome.org/learn/admin-guide/2.14/mimetypes-modifying.html

is the official Gnome documentation on how to accomplish that task)

You should get a MIME type of "application/x-python-bytecode".  If that 
checks out good, then my suggestion would be to place all of your python 
code in a good place (/home/user/scripts or the like) then create a 
desktop shortcut pointing to:

"python /home/user/scripts/targetScript.pyc".

If my memory serves me well, when you create a desktop shortcut you can 
tell it what command to run as well as what commandline arguements to 
pass to the program.  Split it up accordingly.

I've tested that out in idesk running on fluxbox, so I can't guarantee 
how well it will work under Gnome, but it's worth a try.  Also, if your 
script doesn't have a GUI, you'll need to point the desktop shortcut to:

"xterm -e 'python /home/user/scripts/targetScript.pyc'"

(use your favorite terminal, of course...) instead of simply "python 
/home/user/scripts/targetScript.pyc", or you'll lose the output.  (I 
checked against idesk/fluxbox while monitoring with Conky...my CPU 
shoots to 100% on a py script that I wrote that builds Markov models 
from the words in a given dictionary file...(yes, I was quite bored the 
day I wrote that...), so I know the script is indeed running even 
without a terminal to output to).

If you create the shortcut in that fashion, you *should* be able to run 
whatever python bytecode you want...

Hope this helps, and let me know how that works out for you...

Jonathon


Alfonso wrote:
> Jonathon Sisson escribió:
>> Alfonso wrote:
>>   
>>> Sorry for the too obvious question. I'm new to python and have no idea 
>>> how can I make execute a compiled .pyc with a double click in linux, 
>>> with gnome. Trying to double click in a .py gives allways the question 
>>> wether I want to execute the text file, or read it. (This behaviour can 
>>> be changed with gconf-editor, but as it is for security matters, I would 
>>> prefer to execute the .pyc with a double click). I have tried to 
>>> associate python to the .pyc (first time I executed it there was no 
>>> programm associated), but it doesn't work.
>>>
>>>     
>> I don't know how much Linux experience you have (judging by the
>> "double-click" concept, I'm assuming you're coming from a Windows
>> background or are perhaps catering to users with only a Windows
>> background) (correct me if I'm wrong)...so I'm going to break this down
>> as much as I can.
>>
>> .pyc files are considered binary files by Linux.  As such, bash attempts
>> to execute them as binary (ELF, etc...), which obviously won't work.  If
>> you really need to have double-click/execute functionality, consider
>> writing a small shell script to execute the .pyc file for you.
>>
>> For instance, let's say you have a python script "foo.py" and a compiled
>> python script "foo.pyc".  If you attempt to run foo.py from the shell
>> and you have a proper header (i.e. #!/usr/bin/python), then bash can
>> execute the script.  I'm assuming that GNOME has similar functionality
>> (I prefer Fluxbox to either GNOME or KDE), which allows your .py files
>> to execute directly.  .pyc, however, even with a file association, fails
>> to launch...on my system, I get this error:
>>
>> $ ./foo.pyc
>> bash: ./foo.pyc: cannot execute binary file
>>
>> bash recognizes the file as binary, but it fails to launch as an ELF
>> binary (or whatever you're set up to run).  To fix it, simply write a
>> shell script as such:
>>
>> <code>
>> #!/bin/sh
>>
>> python /home/me/scripts/foo.pyc
>>
>> </code>
>>
>> Name the script whatever you want (i.e. foo.sh) then run from the
>> commandline:
>>
>> $ chmod 700 foo.sh
>>
>> This gives the script read/write/execute permissions for the owner of
>> the script...if you require read/write/execute/etc...for group or all,
>> change 700 to whatever you need.  (i.e. 755 for rwxr-xr-x permissions)
>>
>> (Alternatively you can right click on the shell script and set
>> permissions graphically...whichever you prefer)  Now, you should be able
>> to double-click the script (or preferably a shortcut to the script on
>> your desktop), which will launch the compiled python module for you.  If
>> this seems like a lot of work, then perhaps you could write a shell
>> script to automate the task of creating the shell script and setting
>> permissions each time you create a new .pyc...doh?
>>
>> Hope this is a satisfactory answer...and if anyone knows something I
>> have overlooked, please let Alfonso and I know.
>>
>> Jonathon
>>
>>
>>   
>>> Thank you for your answer.
>>>
>>> 		
>>> ______________________________________________ 
>>> LLama Gratis a cualquier PC del Mundo. 
>>> Llamadas a fijos y móviles desde 1 céntimo por minuto. 
>>> http://es.voice.yahoo.com
>>> _______________________________________________
>>> Tutor maillist  -  Tutor at python.org
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>>     
>>   
> Thank you very much for your answer.
> 
> Both true, coming from windows, three years with linux. I don't have any 
> fear to executing from the shell, but It's not the same thing with some 
> of my friends, totally new to linux, and not very much friends of the 
> console. I would like to distribute to them my python programms (when I 
> have the knolowdge to writte something decent with python :) ).
> 
>  If I use a bash script I have the same "problem" that with executing 
> the .py. Gnome asks wether you want to execute or read it, etc. This is 
> due to security reasons, and can be disabled using the tool 
> gconf-editor, you can change that behaviour so that the files are 
> allways executed or opened with a text editor. But I didn't want to 
> change that, and I thought this could be easily avoided just executing 
> the .pyc files, because I have programmed with mono (.net) in linux and 
> that works so. I mean, if I'm not mistaked .pyc are compiled in 
> intermediate language files, just like .exe in .net/mono. With mono all 
> I have to do was chmod at the .exe, then changing the programm with 
> which the .exe is opened (to mono) and that works fine for me, it can be 
> opened with a double click. But it's not an elf binary, I think...
> 
> I think there must be a way to execute with a launcher the programm 
> without being asked each time. As it is for example with bittornado or 
> bittorrent, both written in python and both are executed from the 
> applications menu or from a launcher at the desktop with no intermediate 
> question about if you want to show or execute the file, or just to 
> cancel the action. In fact, when I execute bittornado it's a file called 
> btdownoladgui.bittornado which is executed. But it's python code. If I 
> try to do something similar (executing a file with a not .py extension, 
> but written in python), gnome will complain, that the file is python 
> script with a different extension, and that as it could be a security 
> risk it won't be executed. Could it be that those applications, 
> bittornado, bittorrent, etc., modify some gnome configuration files when 
> they are installed, to avoid that default behaviour?
> 
> Don't know if I'm explaining right (sorry for my bad english). Resume: 
> anyone that has programmed a python app and created a launcher, in 
> gnome, how do you make it so that the file is executed directly after 
> clicking on it without showing all this anoying messages each time?
> 
> Again, thank you for your answers.
> 
> 
> 		
> ______________________________________________ 
> LLama Gratis a cualquier PC del Mundo. 
> Llamadas a fijos y móviles desde 1 céntimo por minuto. 
> http://es.voice.yahoo.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list