Embedding Python's library as zip file

Ian Kelly ian.g.kelly at gmail.com
Thu May 5 17:41:25 EDT 2011


On Thu, May 5, 2011 at 2:34 PM, Wojtek Mamrak <tacyt1007 at gmail.com> wrote:
> Maybe I am missing the point, but I think I am not able to do this.
> When I remove the "Lib" folder and try to run Python.exe, the python
> console window closes rapidly, so that it is hard to read any message
> displayed in it and obvioulsy I can't run any python command. Could
> you clarify this please?

Darn, it worked for me in Python 2.5:

C:\Python25>python
'import site' failed; use -v for traceback
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append(r'C:\Python25\lib.zip')
>>> import site

Although I am also seeing the issue with sys.prefix not getting set
and site-packages not getting added.

But in Python 2.7 I just get:

C:\Python27>python
ImportError: No module named site

C:\Python27>

And in Python 3.2 it causes a full-on crash:

C:\Python32>python
Fatal Python error: Py_Initialize: unable to load the file system codec
LookupError: no codec search functions registered: can't find encoding

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

>> What if you put the zip file in your
>> PYTHONPATH environment variable?
>
> I don't have it defined. Maybe it is because I have 2 different Python
> interpreters installed.
> After adding the PYTHONPATH, the Python.exe seems to run normally.
> Unfortunately I am not able to import Image module (I am using it),
> which is installed in site-packages/PIL.
> This happens, because for some reason the site-packages directory is
> not present in sys.path. After reading the site module description I
> checked the sys.prefix and sys.exec_prefix, which are both empty, when
> the Lib folder is loaded from the zip archive. Thats why site-packages
> is not added to sys.path.
> Of course I can add site-packages directory to the sys.path by myself
> (yet I don't know why sys.prefix and sys.exec_prefix are empty),
> however how to deal with the PYTHONPATH, which is required to load
> site.py and hence has to be defined at the very beginning?

Are you calling Py_SetProgramName?  That may help to set sys.prefix
and sys.exec_prefix.  However, looking at site.py, it appears that
it's only looking for proper directories.  I don't think it will be
able to add a site-packages inside a zip archive at all; you will just
have to add that yourself.



More information about the Python-list mailing list