2

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 simple 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.

phooji
10.4k3 gold badges41 silver badges46 bronze badges
asked Feb 20, 2011 at 22:28

3 Answers 3

3

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.

answered Feb 21, 2011 at 0:29
Sign up to request clarification or add additional context in comments.

1 Comment

Depending on your platform, tools like py2exe and py2app may also be of interest.
1

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

answered Feb 20, 2011 at 23:29

2 Comments

-1 for suggesting a requeriement change instead of responding the actual question: what if the circumstances do not allow packages to be used? In my case, I am working in a kind of application server which allow hot-deployment for flat-file modules, but do not allow runtime changes if multimodule packages are used. Therefore, I need to do EXACTLY what the OP asks for, not change my requirements.
Diveintopython3 link is dead.
0

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.

answered Feb 21, 2011 at 0:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.