Cannot run a specific .pyc file

自古美人都是妖i 提交于 2019-12-04 13:40:57

问题


After compiling a in unix-working python file using

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.

So the question is, how to compile a .py file properly to a .pyc file and how to run this .pyc file?

ps: I did test compiling & running a basic script, which worked..


回答1:


Compiling a python file does not produce an executable, unlike C. You have to interpret the compiled Python code with the Python interpreter.

$ 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.




回答2:


Run the first command to generate the server.pyc file. Then the second command can run the server.pyc module. The -c option and -m option are described in the python docs.

python -c "import server"
python -m server


来源:https://stackoverflow.com/questions/12987818/cannot-run-a-specific-pyc-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!