Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
使用在 unix-working python 文件中编译后
import py_compile py_compile.compile('server.py')
我在同一个目录中获得了 .pyc 文件,但是当我尝试在 putty 中使用 './server.pyc' 运行该文件时,我得到的只是作为输出的加扰代码,并且没有真正发生任何事情。
所以问题是,如何将 .py 文件正确编译为 .pyc 文件,以及如何运行这个 .pyc 文件?
ps:我确实测试了编译和运行一个基本脚本,它有效..
与 C 不同,编译 python 文件不会产生可执行文件。您必须使用 Python 解释器解释编译后的 Python 代码。
$ python >>> import py_compile >>> py_compile.compile('server.py') >>> ^D $ python ./server.pyc
编译后的 Python 代码的唯一变化是加载时间略短。Python 解释器在加载时已经编译了代码,这根本不需要很长时间。
运行第一个命令以生成 server.pyc 文件。然后第二个命令可以运行 server.pyc 模块。python 文档中描述了-c选项和-m 选项。
python -c "import server" python -m server