Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Executable standalone *.pyc after inserting "#!/usr/bin/python" orother options

Reply
Thread Tools

Executable standalone *.pyc after inserting "#!/usr/bin/python" orother options

 
 
epsilon
Guest
Posts: n/a
 
      01-14-2010
All:

I've been playing with "Lua" and found something really cool that I'm
unable to do in "Python". With "Lua", a script can be compiled to byte
code using "luac" and by adding "#!/usr/bin/lua" at the top of the
binary, the byte code becomes a single file executable. After I found
this trick, I ran back to "Python" to give it a try. Well... it
didn't work. Is this possible? There are tools which insert "python"
and related modules inside the byte code, but this is not the
requirement for this situation. The required solution is to insert "#!/
usr/bin/python" (or some string) at the top of a *.pyc byte code file
and run it as standalone executable. I'm excited about the
possibility and interested in hearing your thoughts.

Thank you,
Christopher Smiga
 
Reply With Quote
 
 
 
 
Martin v. Loewis
Guest
Posts: n/a
 
      01-14-2010
> I've been playing with "Lua" and found something really cool that I'm
> unable to do in "Python". With "Lua", a script can be compiled to byte
> code using "luac" and by adding "#!/usr/bin/lua" at the top of the
> binary, the byte code becomes a single file executable. After I found
> this trick, I ran back to "Python" to give it a try. Well... it
> didn't work. Is this possible?


In Python, a different approach will work, depending on the operating
system.

E.g. on Linux, you can use binfmt_misc to make executables out of pyc
code. Run

import imp,sys,string
magic = string.join(["\\x%.2x" % ord(c) for c in imp.get_magic()],"")
reg = 'yc:M::%s::%s:' % (magic, sys.executable)
open("/proc/sys/fs/binfmt_misc/register","wb").write(reg)

once on your Linux system (or, rather, at boot time), and all pyc
files become executable (if the x bit is set).

In Debian, installing the binfmt-support package will do that for
you.

Do "ls /proc/sys/fs/binfmt_misc/" to see what binary types are
already supported on your system.

HTH,
Martin

P.S. The approach you present for Lua indeed does not work for
Python.
 
Reply With Quote
 
 
 
 
epsilon
Guest
Posts: n/a
 
      01-14-2010
On Jan 14, 5:33*pm, "Martin v. Loewis" <mar...@v.loewis.de> wrote:
> > I've been playing with "Lua" and found something really cool that I'm
> > unable to do in "Python". With "Lua", a script can be compiled to byte
> > code using "luac" and by adding "#!/usr/bin/lua" at the top of the
> > binary, the byte code becomes a single file executable. After I found
> > this trick, I ran back to "Python" to give it a try. *Well... *it
> > didn't work. Is this possible?

>
> In Python, a different approach will work, depending on the operating
> system.
>
> E.g. on Linux, you can use binfmt_misc to make executables out of pyc
> code. Run
>
> import imp,sys,string
> magic = string.join(["\\x%.2x" % ord(c) for c in imp.get_magic()],"")
> reg = 'yc:M::%s::%s:' % (magic, sys.executable)
> open("/proc/sys/fs/binfmt_misc/register","wb").write(reg)
>
> once on your Linux system (or, rather, at boot time), and all pyc
> files become executable (if the x bit is set).
>
> In Debian, installing the binfmt-support package will do that for
> you.
>
> Do "ls /proc/sys/fs/binfmt_misc/" to see what binary types are
> already supported on your system.
>
> HTH,
> Martin
>
> P.S. The approach you present for Lua indeed does not work for
> Python.



Martin,

This works great! Do you or anyone else have information on how to do
the same thing for Windows and/or Solaris.

Thank you again,
Christopher
 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      01-15-2010
On 01/15/10 09:33, Martin v. Loewis wrote:
>
> P.S. The approach you present for Lua indeed does not work for
> Python.


Actually the approach should work, though with a little workaround; you
can write your wrapper (e.g. #!/usr/bin/mypython) that simply strips the
first line and pass the file to the real python interpreter
(/usr/bin/python or whatever).

And I think it would be trivial to patch python interpreter to support
loading of #!-ed .pyc.
 
Reply With Quote
 
Martin v. Loewis
Guest
Posts: n/a
 
      01-15-2010
> This works great! Do you or anyone else have information on how to do
> the same thing for Windows and/or Solaris.


On Windows, just associate the .pyc extension with Python - the standard
installation will already do that.

On Solaris, I don't think something like this is supported.

Regards,
Martin
 
Reply With Quote
 
schmeii
Guest
Posts: n/a
 
      01-15-2010
On Jan 14, 10:55*pm, epsilon <cesm...@gmail.com> wrote:
> All:
>
> I've been playing with "Lua" and found something really cool that I'm
> unable to do in "Python". With "Lua", a script can be compiled to byte
> code using "luac" and by adding "#!/usr/bin/lua" at the top of the
> binary, the byte code becomes a single file executable. After I found
> this trick, I ran back to "Python" to give it a try. *Well... *it
> didn't work. Is this possible?


You can't add a string on top of a pyc file but you can add one in a
zipped file. For an example, see http://www.noah.org/wiki/Python_zip_exe

 
Reply With Quote
 
epsilon
Guest
Posts: n/a
 
      01-15-2010
On Jan 15, 8:32*am, schmeii <maxischm...@gmail.com> wrote:
> On Jan 14, 10:55*pm, epsilon <cesm...@gmail.com> wrote:
>
> > All:

>
> > I've been playing with "Lua" and found something really cool that I'm
> > unable to do in "Python". With "Lua", a script can be compiled to byte
> > code using "luac" and by adding "#!/usr/bin/lua" at the top of the
> > binary, the byte code becomes a single file executable. After I found
> > this trick, I ran back to "Python" to give it a try. *Well... *it
> > didn't work. Is this possible?

>
> You can't add a string on top of a pyc file but you can add one in a
> zipped file. For an example, seehttp://www.noah.org/wiki/Python_zip_exe



All:

Thanks again and I'm looking at all the options for different
operating system

Christopher
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python scripts to standalone executable Amit Gupta Python 8 04-01-2008 01:03 PM
Build standalone executable carl.manaster@gmail.com Java 4 10-31-2006 02:53 PM
Cross platform distribution of standalone executable Mahesh Python 2 03-27-2005 08:42 PM
creating standalone java executable Peter Java 13 03-03-2004 10:18 PM
Standalone Perl Executable ? ? ? Dipesh Mehta Perl 3 10-02-2003 02:05 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57