python3.2的pycompile [英] pycompile for python3.2

查看:126
本文介绍了python3.2的pycompile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行mint 13,并使用 apt-get 软件包管理系统安装了python 3.2。我还同时安装了python 2.7和3.2。pycompile似乎是打包python 2.7代码并抛出python 3.2代码异常的代码。

I am running mint 13 and have python 3.2 installed using the apt-get package management system. I also have python 2.7 installed along with 3.2 The pycompile seems to be the one that packages python 2.7 code and and throws exceptions for python 3.2 code.

尝试安装一些软件包,但无法为python 3.2找到pycompile。

I have looked around and tried to install a few packages, but have not been able to find pycompile for python 3.2. How do I get pycompile work for python 3.2?

推荐答案

py_compile 是一个stdlib模块,可以在给定Python源代码的情况下生成字节码。 它很少需要。

要编译Python 3源代码,必须使用其随附的py_compile版本,而不是Python 2.7中的版本。从命令行使用它:

To compile Python 3 source code you must use py_compile version included with it and not the version from Python 2.7 if you use it from a command-line:

$ python3 -mpy_compile your_script.py

要更改存储pyc文件的默认位置,可以使用 cfile 参数a href = http://docs.python.org/release/3.2.3/library/py_compile.html#py_compile.compile rel = nofollow> py_compile.compile()函数。

To change the default location where pyc-files are stored you could use cfile parameter of the py_compile.compile() function.


字节代码是否会使脚本运行得更快?

Does the byte code make the script run any faster?

可能(以微小的量)。 Python编译器并没有做太多事,因此它很快。

It might (by a minuscule amount). Python compiler doesn't do much so it is fast.

下面是一个示例代码,其字节码看起来像是人类可读的形式:

Here's an example how byte-code looks like in a human readable form:

>>> def f(o):
...     with o:
...         pass
...
>>> import dis   
>>> dis.dis(f)
  2           0 LOAD_FAST                0 (o)
              3 SETUP_WITH               5 (to 11)
              6 POP_TOP             

  3           7 POP_BLOCK           
              8 LOAD_CONST               0 (None)
        >>   11 WITH_CLEANUP        
             12 END_FINALLY         
             13 LOAD_CONST               0 (None)
             16 RETURN_VALUE

所有繁重的工作都留给了在运行时解释字节码的python解释器。

All heavy lifting is left for the python interpreter that interprets the byte-code at runtime.


还是仅用于分发?

Or is this only for distribution?

文档提供了一个用例:一个包含Python模块的共享目录,只有窗帘用户可以编写。您可以禁用将字节码缓存到磁盘,以便可以使用存储在只读位置的py文件而没有相应的pyc文件。

The docs provide a use-case: a shared directory with Python modules where only curtain users can write. You can disable caching byte-code to disk so it is possible to use py-files that stored in read-only locations without corresponding pyc-files.


为什么很少使用它?

Why is it rarely used?

通常pyc文件是通过构建/安装过程创建的。如果没有pyc文件,则可以在导入模块时即时创建它们:

Usually the pyc-files are created by building/installation process. If there is no pyc-files they can be created on-the-fly when you import a module:

$ python -c 'import some_module'

这篇关于python3.2的pycompile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆