[Tutor] executing with double click on linux

Jonathon Sisson sisson.j at gmail.com
Wed Oct 11 21:48:46 CEST 2006


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
> 


More information about the Tutor mailing list