因为想尝试在我们的桌面软件中加入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;elsepythonhome=NULL;
}elsestrncpy(prefix, pythonhome, MAXPATHLEN);
1.这个是当pythonhome为NULL的时候,python的初始化函数会调用search_for_prefix来设置默认的home
staticintsearch_for_prefix(char*argv0_path,char*landmark)
{/*Search from argv0_path, until landmark is found*/strcpy(prefix, argv0_path);do{if(gotlandmark(lan dmark))return1;
reduce(prefix);
}while(prefix[0]);return0;
}
2.landmark是一个字符串,里面是lib/os.py,gotlandmark主要是测试下当年目录下的是否有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.*/staticintgotlandmark(char*landmark)
{intok;
Py_ssize_t n;
n=strlen(prefix);
join(prefix, landmark);
ok=ismodule(prefix);
prefix[n]='\0';returnok;
}
3.如果没有,则再往上上找一层目录,继续执行2的步骤,直到找到或者prefix[0]为0后才会停止
那么我们只要把python27下的lib考到客户机上的exe目录下就可以了
把测试程序运行一遍以后,正常,接下来就是精简库了, 基本上把test和unittest,email之类的库删掉就可以了