Skip to content

Instantly share code, notes, and snippets.

@thepaul
Created January 2, 2013 20:23
Show Gist options
  • Save thepaul/4437650 to your computer and use it in GitHub Desktop.
Save thepaul/4437650 to your computer and use it in GitHub Desktop.
make byte-compiled python code into an executable
#!/usr/bin/env python
#
# runnable-pyc
usage_info = \
"""runnable-pyc - make compiled Python code into an executable file
Usage: runnable-pyc <pycfile> [<outputfile>]
If no outputfile is specified, and pycfile ends in '.pyc', then the default
outputfile is the same as pycfile with the '.pyc' chopped off. This program
will not overwrite an existing file, though.
The outputfile will be made mode 0755. This is not yet overrideable.
Note that the output file is just the input file with some special shell
code prepended. It will still require Python (and sh).
"""
def usage():
sys.stderr.write(usage_info)
import sys, os
shellcode = \
"""#!/bin/sh
# created by runnable-pyc
exec %s - "$0" <<'EOShell'
import sys, marshal
f = file(sys.argv[1], 'rb')
f.seek(%%-4s)
exec marshal.load(f)
EOShell
""" % sys.executable
shellcode %= (len(shellcode) + 8)
def make_runnable(fname, outfname):
outf = file(outfname, 'wb')
outf.write(shellcode)
outf.write(file(fname, 'rb').read())
outf.close()
os.chmod(outfname, 0755)
def main():
if len(sys.argv) < 2 or len(sys.argv) > 3:
usage()
sys.exit(1)
if len(sys.argv) == 3:
outfname = sys.argv[2]
elif sys.argv[1].endswith('.pyc'):
outfname = sys.argv[1][:-4]
else:
sys.stderr.write("Don't know what to call output file.\n")
sys.exit(2)
if os.path.exists(outfname):
sys.stderr.write("Output file already exists.\n")
sys.exit(3)
make_runnable(sys.argv[1], outfname)
if __name__ == "__main__":
main()
@James404-cyber
Copy link

When i compiledwith this file than i get like this error
Screenshot_20220322-210247_Termux

@thepaul
Copy link
Author

thepaul commented Mar 22, 2022

This code will only work with Python 2, which is quite dead. I’m not sure how much change it would require to work in Python 3.

@James404-cyber
Copy link

James404-cyber commented Mar 23, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment