如何将python脚本和模块打包到单个脚本中? [英] How can I package a python script and modules into a single script?

查看:76
本文介绍了如何将python脚本和模块打包到单个脚本中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的主脚本和它导入的所有

模块打包成一个单独的脚本,它将像集合一样运行。它不应该是b $ b需要打包标准的Python lib模块 - 只需

我的本地模块。这不是用py2exe或py2app回答的问题

。我不需要将脚本打包成二进制文件和/ b
以及Python。我希望安装Python。

我也不想使用distutils来安装

脚本。该脚本将始终在Unix上运行。


我认为可能有某种方法可以从tar文件运行

脚本包。一种方法是将
的包脚本解压缩到temp

目录中;运行主脚本;完成后删除

临时包目录。这听起来很笨拙,而且如果有人

做了''kill -9'',他们很容易留下垃圾。但是,这可能是一个可接受的折扣。我确定我已经看到了这样的一个

工具,但我再也找不到了。

我查了一下常用的地方(google)和sf.net)。


我还考虑简单地将所需的模块切割并粘贴到一个单一的巨型脚本中,

但这不如tarball想法那么吸引人

(除非它可以自动完成)。


你的,

诺亚

I would like to package my main script and all the
modules it imports into a single script that will
run just as the collection would. It should not
need to package standard Python lib modules -- just
my local modules. This is not the same question
that would be answered with py2exe or py2app. I
don''t need to package the script into a binary
along with Python. I expect Python to be installed.
I also don''t want to use distutils to install the
script. The script will always run on Unix.

I thought that there might be some way to run a
script package from a tar file. One way would be to
have the package script untar itself into a temp
directory; run the main script; then delete the
temporary package directory when done. That sounds
clunky and prone to leave around trash if someone
does a ''kill -9''. However, this might be an
acceptable compromise. I''m sure that I''ve seen a
tool around like this, but I can''t find it anymore.
I checked the usual places (google and sf.net).

I also considered simply cutting and pasting all
the modules I need into one single, giant script,
but that''s less appealing than the tarball idea
(unless it could be done automatically).

Yours,
Noah

推荐答案

" Noah" <无** @ noah.org>写道:
"Noah" <no**@noah.org> writes:
我想将我的主脚本及其导入的所有
模块打包成一个单独的脚本,该脚本将像集合一样运行。它不应该需要打包标准的Python lib模块 - 只需要我的本地模块。这不是用py2exe或py2app回答的问题。我不需要将脚本与Python一起打包成二进制文件。我希望安装Python。
我也不想使用distutils来安装
脚本。该脚本将始终在Unix上运行。

我认为可能有某种方法可以从tar文件运行
脚本包。一种方法是将包脚本解压缩到temp
目录中;运行主脚本;完成后删除
临时包目录。这听起来很笨拙,如果有人杀死-9,就容易留下垃圾。但是,这可能是一个可接受的妥协。我确定我已经看到了这样的工具,但我再也找不到了。
I would like to package my main script and all the
modules it imports into a single script that will
run just as the collection would. It should not
need to package standard Python lib modules -- just
my local modules. This is not the same question
that would be answered with py2exe or py2app. I
don''t need to package the script into a binary
along with Python. I expect Python to be installed.
I also don''t want to use distutils to install the
script. The script will always run on Unix.

I thought that there might be some way to run a
script package from a tar file. One way would be to
have the package script untar itself into a temp
directory; run the main script; then delete the
temporary package directory when done. That sounds
clunky and prone to leave around trash if someone
does a ''kill -9''. However, this might be an
acceptable compromise. I''m sure that I''ve seen a
tool around like this, but I can''t find it anymore.




Wasn''这个名为''squeeze''?


Thomas



Wasn''t this named ''squeeze''?

Thomas


Noah< no ** @ noah.org>写道:
Noah <no**@noah.org> wrote:
我想将我的主脚本和它导入的所有
模块打包成一个单独的脚本,它将像集合一样运行。它不应该需要打包标准的Python lib模块 - 只需要我的本地模块。这不是用py2exe或py2app回答的问题。我
I would like to package my main script and all the
modules it imports into a single script that will
run just as the collection would. It should not
need to package standard Python lib modules -- just
my local modules. This is not the same question
that would be answered with py2exe or py2app. I




用你想要的所有模块的.pyc制作一个zipfile。在它之前放一个小的

文本标题来导入主文并运行它的主要

函数;就是这样,感谢Python能够从zipfiles导入。


Python Cookbook(第2版)中有一个配方。


(我相信py2app也不需要打包标准库,

,因为它附带MacOSX 10.3及更高版本,但我没有检查过) 。

Alex



Make a zipfile with the .pyc of all the modules you want; put a small
text header right before it to import the mainscript and run its main
function; that''s it, thanks to Python''s ability to import from zipfiles.

There''s a recipe for that in the Python Cookbook (2nd edition).

(I believe py2app doesn''t need to package the standard library, either,
since it comes with MacOSX 10.3 and later, but I haven''t checked).
Alex


我相信你正在考虑冻结,这是正常的一部分

python发行版。


还有cx_Freeze(
http://starship.python.net/crew/atuining/cx_Freeze/ )....


Bob

I believe you are thinking of "freeze", which is part of the normal
python distro.

There is also cx_Freeze (
http://starship.python.net/crew/atuining/cx_Freeze/ )....

Bob


这篇关于如何将python脚本和模块打包到单个脚本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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