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

Discussion in 'Python' started by epsilon, Jan 14, 2010.

  1. epsilon

    epsilon Guest

    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
     
    epsilon, Jan 14, 2010
    #1
    1. Advertising

  2. Re: Executable standalone *.pyc after inserting "#!/usr/bin/python"or other options

    > 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 = ':pyc: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 v. Loewis, Jan 14, 2010
    #2
    1. Advertising

  3. epsilon

    epsilon Guest

    Re: Executable standalone *.pyc after inserting "#!/usr/bin/python"or other options

    On Jan 14, 5:33 pm, "Martin v. Loewis" <> 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 = ':pyc: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
     
    epsilon, Jan 14, 2010
    #3
  4. epsilon

    Lie Ryan Guest

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

    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.
     
    Lie Ryan, Jan 15, 2010
    #4
  5. Re: Executable standalone *.pyc after inserting "#!/usr/bin/python"or other options

    > 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
     
    Martin v. Loewis, Jan 15, 2010
    #5
  6. epsilon

    schmeii Guest

    Re: Executable standalone *.pyc after inserting "#!/usr/bin/python"or other options

    On Jan 14, 10:55 pm, epsilon <> 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
     
    schmeii, Jan 15, 2010
    #6
  7. epsilon

    epsilon Guest

    Re: Executable standalone *.pyc after inserting "#!/usr/bin/python"or other options

    On Jan 15, 8:32 am, schmeii <> wrote:
    > On Jan 14, 10:55 pm, epsilon <> 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
     
    epsilon, Jan 15, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Salerno
    Replies:
    30
    Views:
    2,024
    Stephan Kuhagen
    Aug 10, 2006
  2. Yves Dorfsman

    #!/usr/bin/env python vs. #!/usr/bin/python

    Yves Dorfsman, May 2, 2008, in forum: Python
    Replies:
    27
    Views:
    2,090
    Tim Roberts
    May 10, 2008
  3. Chris Rebert

    Re: How to create Standalone PYC File

    Chris Rebert, Mar 4, 2009, in forum: Python
    Replies:
    0
    Views:
    512
    Chris Rebert
    Mar 4, 2009
  4. HoneyMonster
    Replies:
    9
    Views:
    493
    John Roth
    Feb 24, 2012
  5. Gilles
    Replies:
    7
    Views:
    584
    Matej Cepl
    Sep 30, 2012
Loading...

Share This Page