Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing the entire Python standard library

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.

like image 330
Ram Rachum Avatar asked Oct 14 '22 14:10

Ram Rachum


2 Answers

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.

like image 174
Ram Rachum Avatar answered Oct 25 '22 04:10

Ram Rachum


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)

like image 29
lazy1 Avatar answered Oct 25 '22 05:10

lazy1