简体   繁体   English

如何直接在Linux终端中执行* .pyc文件

[英]How to execute *.pyc file directly in linux terminal

I found that on my ubuntu15.10, I can not execute a *.pyc file like 我发现在ubuntu15.10上无法执行* .pyc文件,例如

$ 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. 但是可以在另一台PC上(也是ubuntu15.10)执行它。 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. 在此链接中( https://superuser.com/questions/73615/how-do-pyc-files-execute/73649#73649 ),我知道这是因为我的系统没有.pyc文件的binfmt_misc条目。 So my question is how to add it. 所以我的问题是如何添加它。

I am not sure what distro of Linux you have. 我不确定您拥有什么Linux版本。 For all except Debian based distros, this should work to install the necessary files: 对于除基于Debian的发行版以外的所有发行版,这应该可以安装必需的文件:

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 . 对于Debian,您可以通过安装binfmt-support来安装必要的软件包。

Source 资源

Best of luck, and happy coding! 祝您好运,编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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