Python论坛  - 讨论区

标题:[python-chinese] 请问各位大大,如何手动将.py文件编译为.pyc或者.pyo

2006年10月29日 星期日 23:07

Sky.Li taodev在gmail.com
星期日 十月 29 23:07:51 HKT 2006

如题

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20061029/4f345497/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月30日 星期一 00:10

Linker Lin linkerlin88在gmail.com
星期一 十月 30 00:10:26 HKT 2006

有个complier模块.

>>> import compiler
>>> help(compiler)
Help on package compiler:

NAME
compiler - Package for parsing and compiling Python source code

FILE
c:\python24\lib\compiler\__init__.py

DESCRIPTION
There are several functions defined at the top level that are imported
from modules contained in the package.

parse(buf, mode="exec") -> AST
Converts a string containing Python source code to an abstract
syntax tree (AST). The AST is defined in compiler.ast.

parseFile(path) -> AST
The same as parse(open(path))

walk(ast, visitor, verbose=None)
Does a pre-order walk over the ast using the visitor instance.
See compiler.visitor for details.

compile(source, filename, mode, flags=None, dont_inherit=None)
Returns a code object. A replacement for the builtin compile() function.

compileFile(filename)
Generates a .pyc file by compiling filename.



On 10/29/06, Sky.Li <taodev在gmail.com> wrote:
>
> 如题
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
Linker M Lin
linkerlin88在gmail.com
  ※※※※※※※※※
  ※※我思故我在※※
  ※※※※※※※※※
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20061030/ebdb6087/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月30日 星期一 09:48

马踏飞燕 honeyday.mj在gmail.com
星期一 十月 30 09:48:37 HKT 2006

其实手工写一个更简单。
a.py 是要被编译的
a2.py是一个工具。

a.py的内容
class testa:
    pass

a2.py的内容
import a

这样,运行一下a2.py,那么a.py就被编译成a.pyc了。

2006/10/30, Linker Lin <linkerlin88在gmail.com>:
> 有个complier模块.
>
> >>> import compiler
> >>> help(compiler)
> Help on package compiler:
>
> NAME
> compiler - Package for parsing and compiling Python source code
>
> FILE
> c:\python24\lib\compiler\__init__.py
>
> DESCRIPTION
> There are several functions defined at the top level that are imported
> from modules contained in the package.
>
> parse(buf, mode="exec") -> AST
> Converts a string containing Python source code to an abstract
> syntax tree (AST). The AST is defined in compiler.ast.
>
> parseFile(path) -> AST
> The same as parse(open(path))
>
> walk(ast, visitor, verbose=None)
> Does a pre-order walk over the ast using the visitor instance.
> See compiler.visitor for details.
>
> compile(source, filename, mode, flags=None, dont_inherit=None)
> Returns a code object. A replacement for the builtin compile() function.
>
> compileFile(filename)
> Generates a .pyc file by compiling filename.
>
>
>
> On 10/29/06, Sky.Li <taodev在gmail.com> wrote:
> >
> >
> >
> >
> > 如题
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> >
>
>
>
> --
> Linker M Lin
> linkerlin88在gmail.com
>   ※※※※※※※※※
>   ※※我思故我在※※
>   ※※※※※※※※※
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月30日 星期一 10:16

amingsc amingsc在gmail.com
星期一 十月 30 10:16:53 HKT 2006

哈哈,老马的这个方法很简便
不过有个疑惑,好像import会运行文件中的代码
如果脚本里面直接实现一些操作性的代码
比如复制/删除文件的而且没有if __name__ == '__main__'测试
那么可能会超出原始的期望,效率还很低
当然了,没有几个人会这么做

所以还是用complier吧又不麻烦

马踏飞燕 写道:
> 其实手工写一个更简单。
> a.py 是要被编译的
> a2.py是一个工具。
>
> a.py的内容
> class testa:
>     pass
>
> a2.py的内容
> import a
>
> 这样,运行一下a2.py,那么a.py就被编译成a.pyc了。
>
> 2006/10/30, Linker Lin <linkerlin88 at gmail.com>:
>   
>> 有个complier模块.
>>
>>     
>>>>> import compiler
>>>>> help(compiler)
>>>>>           
>> Help on package compiler:
>>
>> NAME
>> compiler - Package for parsing and compiling Python source code
>>
>> FILE
>> c:\python24\lib\compiler\__init__.py
>>
>> DESCRIPTION
>> There are several functions defined at the top level that are imported
>> from modules contained in the package.
>>
>> parse(buf, mode="exec") -> AST
>> Converts a string containing Python source code to an abstract
>> syntax tree (AST). The AST is defined in compiler.ast.
>>
>> parseFile(path) -> AST
>> The same as parse(open(path))
>>
>> walk(ast, visitor, verbose=None)
>> Does a pre-order walk over the ast using the visitor instance.
>> See compiler.visitor for details.
>>
>> compile(source, filename, mode, flags=None, dont_inherit=None)
>> Returns a code object. A replacement for the builtin compile() function.
>>
>> compileFile(filename)
>> Generates a .pyc file by compiling filename.
>>
>>
>>
>> On 10/29/06, Sky.Li <taodev at gmail.com> wrote:
>>     
>>>
>>>
>>> 如题
>>> _______________________________________________
>>> python-chinese
>>> Post: send python-chinese at lists.python.cn
>>> Subscribe: send subscribe to
>>>       
>> python-chinese-request at lists.python.cn
>>     
>>> Unsubscribe: send unsubscribe to
>>>       
>> python-chinese-request at lists.python.cn
>>     
>>> Detail Info:
>>>       
>> http://python.cn/mailman/listinfo/python-chinese
>>     
>>
>> --
>> Linker M Lin
>> linkerlin88 at gmail.com
>>   ※※※※※※※※※
>>   ※※我思故我在※※
>>   ※※※※※※※※※
>> _______________________________________________
>> python-chinese
>> Post: send python-chinese at lists.python.cn
>> Subscribe: send subscribe to
>> python-chinese-request at lists.python.cn
>> Unsubscribe: send unsubscribe to
>> python-chinese-request at lists.python.cn
>> Detail Info:
>> http://python.cn/mailman/listinfo/python-chinese
>>
>>     
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月30日 星期一 16:53

boyeestudio boyee118在gmail.com
星期一 十月 30 16:53:44 HKT 2006

Âí´ó¸çµÄÕâÖÖ·½·¨£¬ÄÜÉú³É.pyoµÄÎļþÂð£¿
ÎÒÔõôֻÄÜÉú³É.pycµÄÎļþ°¡£¡

2006/10/30, Âí̤·ÉÑà <honeyday.mj在gmail.com>:
>
> ÆäʵÊÖ¹¤Ð´Ò»¸ö¸ü¼òµ¥¡£
> a.py ÊÇÒª±»±àÒëµÄ
> a2.pyÊÇÒ»¸ö¹¤¾ß¡£
>
> a.pyµÄÄÚÈÝ
> class testa:
>     pass
>
> a2.pyµÄÄÚÈÝ
> import a
>
> ÕâÑù£¬ÔËÐÐÒ»ÏÂa2.py£¬ÄÇôa.py¾Í±»±àÒë³Éa.pycÁË¡£
>
> 2006/10/30, Linker Lin <linkerlin88在gmail.com>:
> > ÓиöcomplierÄ£¿é.
> >
> > >>> import compiler
> > >>> help(compiler)
> > Help on package compiler:
> >
> > NAME
> > compiler - Package for parsing and compiling Python source code
> >
> > FILE
> > c:\python24\lib\compiler\__init__.py
> >
> > DESCRIPTION
> > There are several functions defined at the top level that are imported
> > from modules contained in the package.
> >
> > parse(buf, mode="exec") -> AST
> > Converts a string containing Python source code to an abstract
> > syntax tree (AST). The AST is defined in compiler.ast.
> >
> > parseFile(path) -> AST
> > The same as parse(open(path))
> >
> > walk(ast, visitor, verbose=None)
> > Does a pre-order walk over the ast using the visitor instance.
> > See compiler.visitor for details.
> >
> > compile(source, filename, mode, flags=None, dont_inherit=None)
> > Returns a code object. A replacement for the builtin compile() function.
> >
> > compileFile(filename)
> > Generates a .pyc file by compiling filename.
> >
> >
> >
> > On 10/29/06, Sky.Li <taodev在gmail.com> wrote:
> > >
> > >
> > >
> > >
> > > ÈçÌâ
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to
> > python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> > >
> >
> >
> >
> > --
> > Linker M Lin
> > linkerlin88在gmail.com
> >   ¡ù¡ù¡ù¡ù¡ù¡ù¡ù¡ù¡ù
> >   ¡ù¡ùÎÒ˼¹ÊÎÒÔÚ¡ù¡ù
> >   ¡ù¡ù¡ù¡ù¡ù¡ù¡ù¡ù¡ù
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> > python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061030/213963a0/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月30日 星期一 17:31

马踏飞燕 honeyday.mj在gmail.com
星期一 十月 30 17:31:55 HKT 2006

可以生成pyo文件。
但是需要写一个批处理或者在命令行输入:

python -O a1.py

那个是大写的英文字母O,不是0,呵呵。
这样就会出来一个a.pyo了。

在 06-10-30,boyeestudio<boyee118在gmail.com> 写道:
> 马大哥的这种方法,能生成.pyo的文件吗?
> 我怎么只能生成.pyc的文件啊!
>
> 2006/10/30, 马踏飞燕 <honeyday.mj在gmail.com>:
> > 其实手工写一个更简单。
> > a.py 是要被编译的
> > a2.py是一个工具。
> >
> > a.py的内容
> > class testa:
> >     pass
> >
> > a2.py的内容
> > import a
> >
> > 这样,运行一下a2.py,那么a.py就被编译成a.pyc了。
> >
> > 2006/10/30, Linker Lin < linkerlin88在gmail.com>:
> > > 有个complier模块.
> > >
> > > >>> import compiler
> > > >>> help(compiler)
> > > Help on package compiler:
> > >
> > > NAME
> > > compiler - Package for parsing and compiling Python source code
> > >
> > > FILE
> > > c:\python24\lib\compiler\__init__.py
> > >
> > > DESCRIPTION
> > > There are several functions defined at the top level that are imported
> > > from modules contained in the package.
> > >
> > > parse(buf, mode="exec") -> AST
> > > Converts a string containing Python source code to an abstract
> > > syntax tree (AST). The AST is defined in compiler.ast.
> > >
> > > parseFile(path) -> AST
> > > The same as parse(open(path))
> > >
> > > walk(ast, visitor, verbose=None)
> > > Does a pre-order walk over the ast using the visitor instance.
> > > See compiler.visitor for details.
> > >
> > > compile(source, filename, mode, flags=None, dont_inherit=None)
> > > Returns a code object. A replacement for the builtin compile() function.
> > >
> > > compileFile(filename)
> > > Generates a .pyc file by compiling filename.
> > >
> > >
> > >
> > > On 10/29/06, Sky.Li <taodev在gmail.com> wrote:
> > > >
> > > >
> > > >
> > > >
> > > > 如题
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese在lists.python.cn
> > > > Subscribe: send subscribe to
> > > python-chinese-request在lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > > python-chinese-request在lists.python.cn
> > > > Detail Info:
> > > http://python.cn/mailman/listinfo/python-chinese
> > > >
> > >
> > >
> > >
> > > --
> > > Linker M Lin
> > > linkerlin88在gmail.com
> > >   ※※※※※※※※※
> > >   ※※我思故我在※※
> > >   ※※※※※※※※※
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to
> > > python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request在lists.python.cn
> > > Detail Info:
> > > http://python.cn/mailman/listinfo/python-chinese
> > >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月31日 星期二 00:55

马踏飞燕 honeyday.mj在gmail.com
星期二 十月 31 00:55:38 HKT 2006

又仔细看了一下,发现pyc和pyo文件是一样大的,遂拿出来winhex做了一番比较,把能找到的pyc和pyo都作了一下比较,发现内容都是一样的。
也就是说,生成的pyc改个扩展名为pyo就行了。
不过据说pyo是pyc的优化版本,但是目前还没看出来有啥区别,估计是我看得文件太少了?谁研究的比较深入能分享一下经验?

在 06-10-30,马踏飞燕<honeyday.mj在gmail.com> 写道:
> 可以生成pyo文件。
> 但是需要写一个批处理或者在命令行输入:
>
> python -O a1.py
>
> 那个是大写的英文字母O,不是0,呵呵。
> 这样就会出来一个a.pyo了。
>
> 在 06-10-30,boyeestudio<boyee118在gmail.com> 写道:
> > 马大哥的这种方法,能生成.pyo的文件吗?
> > 我怎么只能生成.pyc的文件啊!
> >
> > 2006/10/30, 马踏飞燕 <honeyday.mj在gmail.com>:
> > > 其实手工写一个更简单。
> > > a.py 是要被编译的
> > > a2.py是一个工具。
> > >
> > > a.py的内容
> > > class testa:
> > >     pass
> > >
> > > a2.py的内容
> > > import a
> > >
> > > 这样,运行一下a2.py,那么a.py就被编译成a.pyc了。
> > >
> > > 2006/10/30, Linker Lin < linkerlin88在gmail.com>:
> > > > 有个complier模块.
> > > >
> > > > >>> import compiler
> > > > >>> help(compiler)
> > > > Help on package compiler:
> > > >
> > > > NAME
> > > > compiler - Package for parsing and compiling Python source code
> > > >
> > > > FILE
> > > > c:\python24\lib\compiler\__init__.py
> > > >
> > > > DESCRIPTION
> > > > There are several functions defined at the top level that are imported
> > > > from modules contained in the package.
> > > >
> > > > parse(buf, mode="exec") -> AST
> > > > Converts a string containing Python source code to an abstract
> > > > syntax tree (AST). The AST is defined in compiler.ast.
> > > >
> > > > parseFile(path) -> AST
> > > > The same as parse(open(path))
> > > >
> > > > walk(ast, visitor, verbose=None)
> > > > Does a pre-order walk over the ast using the visitor instance.
> > > > See compiler.visitor for details.
> > > >
> > > > compile(source, filename, mode, flags=None, dont_inherit=None)
> > > > Returns a code object. A replacement for the builtin compile() function.
> > > >
> > > > compileFile(filename)
> > > > Generates a .pyc file by compiling filename.
> > > >
> > > >
> > > >
> > > > On 10/29/06, Sky.Li <taodev在gmail.com> wrote:
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > 如题
> > > > > _______________________________________________
> > > > > python-chinese
> > > > > Post: send python-chinese在lists.python.cn
> > > > > Subscribe: send subscribe to
> > > > python-chinese-request在lists.python.cn
> > > > > Unsubscribe: send unsubscribe to
> > > > python-chinese-request在lists.python.cn
> > > > > Detail Info:
> > > > http://python.cn/mailman/listinfo/python-chinese
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Linker M Lin
> > > > linkerlin88在gmail.com
> > > >   ※※※※※※※※※
> > > >   ※※我思故我在※※
> > > >   ※※※※※※※※※
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese在lists.python.cn
> > > > Subscribe: send subscribe to
> > > > python-chinese-request在lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > > > python-chinese-request在lists.python.cn
> > > > Detail Info:
> > > > http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to
> > python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> > python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月31日 星期二 08:30

limodou limodou在gmail.com
星期二 十月 31 08:30:36 HKT 2006

On 10/31/06, 马踏飞燕 <honeyday.mj在gmail.com> wrote:
> 又仔细看了一下,发现pyc和pyo文件是一样大的,遂拿出来winhex做了一番比较,把能找到的pyc和pyo都作了一下比较,发现内容都是一样的。
> 也就是说,生成的pyc改个扩展名为pyo就行了。
> 不过据说pyo是pyc的优化版本,但是目前还没看出来有啥区别,估计是我看得文件太少了?谁研究的比较深入能分享一下经验?
>
字节码可能是一样的,也许是去掉了象docstring之类的东西吧,不是很清楚。


-- 
I like python!
UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad
My Blog: http://www.donews.net/limodou

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月31日 星期二 09:10

hailang_0512 hailang_0512在163.com
星期二 十月 31 09:10:23 HKT 2006

 python -O a1.py怎么没法编译啊,倒是把脚本直接运行了。我用的compile倒是可以。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月31日 星期二 10:02

马踏飞燕 honeyday.mj在gmail.com
星期二 十月 31 10:02:17 HKT 2006

2006/10/31, hailang_0512 <hailang_0512在163.com>:
>  python -O a1.py怎么没法编译啊,倒是把脚本直接运行了。我用的compile倒是可以。
> _______________________________________________

看我之前的邮件。
a1.py只有一个import语句,是用来编译a.py的。运行的话可以出来a.pyo,而被运行的a1.py没有任何变化的。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月31日 星期二 10:05

马踏飞燕 honeyday.mj在gmail.com
星期二 十月 31 10:05:38 HKT 2006

2006/10/31, limodou <limodou在gmail.com>:
> 字节码可能是一样的,也许是去掉了象docstring之类的东西吧,不是很清楚。
>
可能吧,不过目前倒是没有看出来有何不同。
字节数一样,内容一样。搜索了半天也没发现有人说明白这个问题的。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年10月31日 星期二 10:49

3751 lwm3751在gmail.com
星期二 十月 31 10:49:11 HKT 2006

是象limodou说的那样,文档里面有说明。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号