Python 无法运行特定的 .pyc 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12987818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot run a specific .pyc file
提问by Lazykiddy
After compiling a in unix-working python file using
在使用 unix-working python 文件编译后
import py_compile
py_compile.compile('server.py')
I get the .pyc file in the same directory, but when I try to run this file using './server.pyc' in putty all I get is scrambled code as an output and nothing really happens.
我在同一个目录中得到了 .pyc 文件,但是当我尝试在 putty 中使用 './server.pyc' 运行这个文件时,我得到的只是作为输出的加扰代码,并没有真正发生任何事情。
So the question is, how to compile a .py file properly to a .pyc file and how to run this .pyc file?
所以问题是,如何将 .py 文件正确编译为 .pyc 文件以及如何运行此 .pyc 文件?
ps: I did test compiling & running a basic script, which worked..
ps:我确实测试了编译和运行一个基本脚本,它有效..
采纳答案by Dietrich Epp
Compiling a python file does not produce an executable, unlike C. You have to interpret the compiled Python code with the Python interpreter.
与 C 不同,编译 python 文件不会生成可执行文件。您必须使用 Python 解释器来解释编译后的 Python 代码。
$ python
>>> import py_compile
>>> py_compile.compile('server.py')
>>> ^D
$ python ./server.pyc
The only change compiled Python code has is that it takes slightly less time to load. The Python interpreter already compiles code when it is loaded, and that doesn't take very long at all.
编译后的 Python 代码唯一的变化是加载时间略短。Python 解释器在加载时就已经编译了代码,而且这根本不需要很长时间。

