简体   繁体   中英

How to execute *.pyc file directly in linux terminal

I found that on my ubuntu15.10, I can not execute a *.pyc file like

$ echo 'print "Hello"' > print.py
$ chmod +x print.py
$ python -c 'import print'
$ ./print.pyc

But on another PC (ubuntu15.10 too), it can be executed. In this link( https://superuser.com/questions/73615/how-do-pyc-files-execute/73649#73649 ), I know that it was because my system has not a binfmt_misc entry for .pyc files. So my question is how to add it.

I am not sure what distro of Linux you have. For all except Debian based distros, this should work to install the necessary files:

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)

For Debian, you can install the necessary packages by installing binfmt-support .

Source

Best of luck, and happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM