运行已编译的python脚本的成语? [英] Idiom for running compiled python scripts?

查看:68
本文介绍了运行已编译的python脚本的成语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,并且正在寻找一个更好的习惯用法来使用我已经组织我的python脚本的方式。我已经用Google搜索了关于这个的地方但是发现什么都没有。


我是linux / unix命令行的人,在shell中经验丰富脚本

等我有一堆命令行实用程序脚本,我直接运行。

创建python命令行脚本但利用的最佳方法是什么?
python编译代码的(loadonly)加速优势?


Eg说我有一个python脚本myprog.py。我可以直接每次执行

,但这意味着它是已编译的。每次我运行它都是
哪个效率不高并且会增加启动时间。我一直在创建一个

免费脚本myprog刚才的存根:


#!/ usr / bin / env python

来自myprog import main

if __name__ == " __ main __":

main()

当然在第一次运行时将myprog.py编译成myprog.p,因为我是

想要。


我到目前为止创建的所有python脚本都有这些存根中的一个。

有没有更好的方法?我是否必须创建一个单独的存根每个

时间?我发现每个

实用程序需要一对脚本有点麻烦,它也会造成一些低效率。鉴于上面的存根

是如此样板,为什么python没有为此提供一般存根/实用工具

机制?

解决方案

Mark写道:


Eg说我有一个python脚本myprog.py。我可以直接每次执行

,但这意味着它是已编译的。每次

我运行它,效率不高,增加了启动时间。



你是否衡量了你的情况下的性能?


我有一个这样的存根到目前为止,我创建了我的python脚本

。有没有更好的方法?我每次都必须创建一个单独的

存根吗?我发现每个实用程序需要一对脚本

有点乱,它也会造成一些低效率。鉴于

上面的存根是如此的样板,为什么python没有为此提供一个/或
通用存根/实用程序机制?



我注意到用预编译的pyc

文件调用解释器也有效。


问候,

Bj?

-

BOFH借口#68:


仅在需要知道的基础上提供


Mark< ma ****** @ mailinator.comwrote:


....


#!/ usr / bin / env python

来自myprog import main

if __name__ ==" __ main __":

main()


当然这首先将myprog.py编译成myprog.pyc运行为

我想要。


我为我创建的所有python脚本都有这些存根之一

至今。有没有更好的方法?我每次都必须创建一个单独的

存根吗?我发现每个实用程序需要一对

脚本有点麻烦,它也会导致一些无效的b / b
。鉴于上面的stub是如此的样板,为什么
python没有提供一般的存根/实用机制呢?



我不知道更好的组织方式,但作为一个

替代方案,你可以有一个脚本在哪里导入你想要编译的每个

脚本,python会在你运行它时将编译后的文件写入磁盘(因此避免需要拆分另一个

脚本)。还有py_compile和compileall模块,

有生成字节码的工具。


更多信息:

http://effbot.org/zone/python-compile.htm <在''将python模块编译为字节代码''下的


max


因此,由于缺乏响应,似乎可能没有这样的问题,而且我不应该担心直接运行.py脚本的内部运行.b脚本固有的低效率?


我做了一些时间测试,当然,速度提升很轻微,但是它的收益也是很少。

Hi, I''m new to python and looking for a better idiom to use for the
manner I have been organising my python scripts. I''ve googled all over
the place about this but found absolutely nothing.

I''m a linux/unix command line guy quite experienced in shell scripts
etc. I have a heap of command line utility scripts which I run directly.
What is the best way to create python command line scripts but exploit
the (loadonly) speed-up benefit of python compiled code?

E.g. say I have a python script "myprog.py". I could just execute that
directly each time but that means it is "compiled" each time I run it
which is not efficient and adds to startup time. I have been creating a
complimentary script "myprog" stub which just does:

#!/usr/bin/env python
from myprog import main
if __name__ == "__main__":
main()

Of course this compiles myprog.py into myprog.pyc on first run as I am
wanting.

I have one of these stubs for all my python scripts I''ve created so far.
Is there not a better way? Do I have to create a separate stub each
time? I find it a bit messy to require a pair of scripts for each
utility and it also contributes some inefficiency. Given the above stub
is so boilerplate, why does python not provide a general stub/utility
mechanism for this?

解决方案

Mark wrote:

E.g. say I have a python script "myprog.py". I could just execute
that directly each time but that means it is "compiled" each time
I run it which is not efficient and adds to startup time.

Did you measure the performance hit in your case?

I have one of these stubs for all my python scripts I''ve created
so far. Is there not a better way? Do I have to create a separate
stub each time? I find it a bit messy to require a pair of scripts
for each utility and it also contributes some inefficiency. Given
the above stub is so boilerplate, why does python not provide a
general stub/utility mechanism for this?

I''ve noticed that calling the interpreter with pre-compiled pyc
files also works.

Regards,
Bj?rn
--
BOFH excuse #68:

only available on a need to know basis


Mark <ma******@mailinator.comwrote:

....

#!/usr/bin/env python
from myprog import main
if __name__ == "__main__":
main()

Of course this compiles myprog.py into myprog.pyc on first run as
I am wanting.

I have one of these stubs for all my python scripts I''ve created
so far. Is there not a better way? Do I have to create a separate
stub each time? I find it a bit messy to require a pair of
scripts for each utility and it also contributes some
inefficiency. Given the above stub is so boilerplate, why does
python not provide a general stub/utility mechanism for this?

I don''t know of a better way to organize things, but as an
alternative, you could have a script where you import each of the
scripts that you want compiled, python will write the compiled files
to disk when you run it(thus avoiding the need to split the other
scripts). There are also the py_compile and compileall modules, which
have facilities for generating byte code.

More here:

http://effbot.org/zone/python-compile.htm

under ''Compiling python modules to byte code''.
max


So given the lack of response it seems that there is probably no such
idiom and that I should not be concerned by the inefficiency inherent in
running .py scripts directly?

I did some time tests and sure, the speed gain is slight, but it is a
gain none the less.


这篇关于运行已编译的python脚本的成语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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