To import modules from a .zip
file, you need to add that file to sys.path
- then it will act as a search directory. The zipimport
module that does the job is a built-in one.
sys.path
is constructed like this:
So, you can move all the standard modules into the predefined .zip
file. But you may need to leave an os.py
or lib-dynload
if sys.prefix
and sys.exec_prefix
become blank after that (the contents are irrelevant, the moved modules will be imported from the .zip
because it's earlier on sys.path
), or you will lose access to all 3rd-party modules.
Subdirectories that have their own entry in sys.path
you need to handle separately so that their contents can still be found on sys.path
.
(tested in Python 2.7-win32
)
Though adding .pyc
files to the archive is sufficient, pdb
and stacktraces will be useless unless you place .py
s there as well.