Shebang for compiled Python code
python, shebang
Solution
Shebang works only for text scripts, not binary files. Nevertheless, you can use `binfmt_misc` to execute `*.pyc` files directly, as reported in this Python ML thread:
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.
(emphasis is mine, note that this will apply to all Debian derivatives, including Ubuntu. The same solution works in Fedora too).
Problem
I used to add shebang line at top of Python script as, ``` #!/usr/bin/python ... ``` And I can execute the my.py file by, ``` chmod a+r my.py ./my.py ``` But after compiled to bytecode, the script can only be executed by python and the shebang does not work anymore. ``` python my.pyc ``` Is there anyway to make shebang workable to compiled python script? ``` ./my.pyc ```