开发者

Bundling Multiple Python Modules

I have some Python modules, some of them require more than 20 others. My question is, if there a tool is which helps me to bundle some Python modules to one big file.

Here a s开发者_如何学Goimple example:

HelloWorld.py:

import MyPrinter

MyPrinter.displayMessage("hello")

MyPrinter.py:

def displayMessage(msg):
    print msg

should be converted to one file, which contains:

def displayMessage(msg):
    print msg

displayMessage("hello")

Ok, I know that this example is a bit bad, but i hope that someone understand what i mean and can help me. And one note: I talk about huge scripts with very much imports, if they were smaller I could do that by myself.

Thanks.


Aassuming you are using Python 2.6 or later, you could package the scripts into a zip file, add a __main__.py and run the zip file directly.

If you really want to collapse everything down to a single file, I expect you're going to have to write it yourself. The source code transformation engine in lib2to3 may help with the task.


You cannot and should not 'convert them into one file'.

If your application consists of several modules, you should just organize it into package.

There is pretty good tutorial on packages here: http://diveintopython3.org/packaging.html

And you should read docs on it here: http://docs.python.org/library/distutils.html


Pip supports bundling. This is an installation format, and will unpack into multiple files. Anything else would be a bad idea, as it would break imports and per-module metadata.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜