首页 > 代码库 > 如何把python最小化安装在客户机上面

如何把python最小化安装在客户机上面

因为想尝试在我们的桌面软件中加入python支持,所以想简化python的库,到时候直接放到客户机上面,并且放到我们的目录下,尽量免去不必要的东西,也不要影响机子,不过当写好的程序放到测试机子上后,老是说找不到no module named site,后来看下了代码,发现是找不到site这个库,这个库也是python初始化的时候需要用到的库,解决方法:

1.尝试把我电脑上的python里面的Lib放到客户机上程序的目录,建立一个python27/lib/,结果还是失败

2.lib下的东西放到跟exe同一个目录,可以,但是还是谁显示import pbk_resource错误,但已经可以运行python的解释器了,不过因为跟exe在同一个目录太杂,所以放弃这种方法

 

 

 

后来看了Py_Initialize的代码,发现在没有设置PYTHONHOME的时候,python是这样查找lib:

如下代码

 

 if (pythonhome == NULL || *pythonhome == \0) {        if (search_for_prefix(argv0_path, LANDMARK))            pythonhome = prefix;        else            pythonhome = NULL;    }    else        strncpy(prefix, pythonhome, MAXPATHLEN);

 

1.这个是当pythonhomeNULL的时候,python的初始化函数会调用search_for_prefix来设置默认的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是一个字符串,里面是lib/os.pygotlandmark主要是测试下当年目录下的是否有lib/os.py

 

/* gotlandmark only called by search_for_prefix, which ensures   ‘prefix‘ is null terminated in bounds.  join() ensures   ‘landmark‘ can not overflow prefix if too long.*/static intgotlandmark(char *landmark){    int ok;    Py_ssize_t n;    n = strlen(prefix);    join(prefix, landmark);    ok = ismodule(prefix);    prefix[n] = \0;    return ok;}

 

3.如果没有,则再往上上找一层目录,继续执行2的步骤,直到找到或者prefix[0]0后才会停止

 

那么我们只要把python27下的lib考到客户机上的exe目录下就可以了

 

把测试程序运行一遍以后,正常,接下来就是精简库了, 基本上把test和unittest,email之类的库删掉就可以了

如何把python最小化安装在客户机上面