Because we want to try to add Python support to our desktop software, we want to simplify the Python library, then put it directly on the client, and put it in our directory, try to avoid unnecessary things, do not affect the machine, But when the written program put on the test machine, always said no module named Site, later looked at the code, found that the site is not found This library, this library is also python The library to use when initializing, the workaround :
1. Try to put the Lib in Python on my computer into the directory of the Program on the client, build a python27/lib/, and fail theresult .
2. Put the things under Lib in the same directory as EXE, but who still shows the import Pbk_resource error, but can already run the Python interpreter, But because with EXE in the same directory too miscellaneous, so abandon this method
Later looked at the Py_initialize code, found that in the absence of setting pythonhome,Python is the way to find lib :
The following code
if ' / ' ) { if (Search_for_prefix (Argv0_path, LANDMARK)) = prefix; Else = NULL; } Else strncpy (prefix, pythonhome, maxpathlen);
1. this is when pythonhome is null , the Python initialization function calls Search_for_prefix to set the default home
Static intSearch_for_prefix (Char*argv0_path,Char*landmark) { /*Search from Argv0_path, until landmark is found*/strcpy (prefix, argv0_path); Do { if(Gotlandmark (LAN Dmark))return 1; reduce (prefix); } while(prefix[0]); return 0;}
2.landmark is a string, inside is lib/os.py,Gotlandmark is mainly tested under the current year directory of whether there is lib/os.py
/*Gotlandmark only called by Search_for_prefix, which ensures ' prefix ' are null terminated in bounds. Join () ensures ' landmark ' can not overflow prefix if too long.*/Static intGotlandmark (Char*landmark) { intOK; py_ssize_t N; N=strlen (prefix); Join (prefix, landmark); OK=ismodule (prefix); Prefix[n]=' /'; returnOK;}
3. if not, then go to the top of the directory , continue to perform 2 steps until you find or prefix[0] 0 will not stop
Then we just have to python27 Lib to the client on the exe directory can be
Run the test program once again, normal, the next is to simplify the library, basically the test and unittest,email such as the library to delete it.
How to minimize the Python installation on the client