【问题标题】:How to execute *.pyc file directly in linux terminal如何在 linux 终端中直接执行 *.pyc 文件
【发布时间】:2016-03-04 03:46:05
【问题描述】:

我发现在我的 ubuntu15.10 上,我无法像这样执行 *.pyc 文件

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

但是在另一台PC(ubuntu15.10也是)上可以执行。在这个链接中( https://superuser.com/questions/73615/how-do-pyc-files-execute/73649#73649),我知道这是因为我的系统没有 .pyc 文件的 binfmt_misc 条目。所以我的问题是如何添加它。

【问题讨论】:

    标签: python linux


    【解决方案1】:

    我不确定你有什么 Linux 发行版。对于除基于 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)
    

    对于 Debian,您可以通过安装 binfmt-support 来安装必要的软件包。

    Source

    祝你好运,编码愉快!

    【讨论】:

    • @赵鹏宇 没问题!如果解决方案是正确的,请记得标记为以后的程序员。
    猜你喜欢
    • 1970-01-01
    • 2018-09-11
    • 2012-06-18
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2014-07-03
    • 1970-01-01
    相关资源
    最近更新 更多