views:

233

answers:

2

I need a way to import the entire Python standard library into my program.

While this may seems like a bad idea, I want to do this is so py2exe will package the entire standard library with my program, so my users could import from it in the shell that I give them.

Is there an easy way to do this?

Bonus points: I would prefer that this action will NOT import the packages I have installed in site-packages and which did not come with Python. However, this is not critical.

+1  A: 

Hey, I just thought of something: I only need a list of all the modules in stdlib, and then I'll automatically generate a Python script that imports each of them "manually", like this:

import re
import math
import time
# ...

And then include that with my program.

So all I need now is an easily formatted list of all the modules/packages in stdlib. Now how do I get that?

UPDATE:

I got the list like this: I installed Python 2.6 on a virtual machine, then ran in IDLE:

import pkgutil
stuff = [thing[1] for thing in pkgutil.iter_modules()]
stuff.sort() # To make it easy to look through
print(stuff)

Then copy pasted the output into my IDE, and made a little script to write:

if False:
    import re
    import email
    import time
    # ...

Into a Python module which I import in my program.

It works! py2exe packs the entire stdlib.

UPDATE:

I created a package that does this. I would upload it here but since I don't see any upload button, you can get it off my project folder:

http://github.com/cool-RR/PythonTurtle/tree/master

It's in the folder src, the package is called almostimportstdlib and it's documented.

cool-RR
fortran
I would dynamically generate it personally. Look at what I said above: you can use the same method to obtain the listing of modules to include and generate a file from that. It would be better than having to maintain one manually.
jkp
+1  A: 

I created a zip file from all the Python standard library and then added it to sys.path when the program started.

You can have a look at the sources here (abandoned project)

lazy1
V.good idea. You should be able to bundle it as a resource using py2exe and add it to the path after the app loads: I know you could do this with PyInstaller at least.
jkp