python打包成可执行文件_osdba's blog : 把python脚本打包成二进制可执行程序的工具...

本文介绍了将Python脚本打包成二进制可执行程序的工具。阐述了工具的使用方法,给出了示例,先创建测试脚本test01.py,经打包程序生成C语言代码test01.c,最后还提及了工具的下载地址。

把python脚本打包成二进制可执行程序的工具

Posted on 2015-05-12 18:42:29 by osdba

1. 工具的使用方法

此打包工具把python源代码打包成一个可执行的二进制文件。

$./pkgpy.py

Usage: ./pkgpy.py

2. 示例

先建测试的脚本test01.py,内容如下:

#cat test01.py

#!/usr/bin/env python

# -*- coding:UTF-8

'''

Created on 2015-04-14

@author: tangcheng

'''

import sys

import os

import traceback

def main():

print "Hello world"

if __name__ == "__main__":

main()

测试过程如下:

$./pkgpy.py test01.py test01

Run: gcc -I/usr/include/python2.6 -lpython2.6 test01.c -o test01

$./test01

Hello world

实际上此打包程序把test01.py生成了如下C语言代码test01.c:

#include

#include

static int g_file_size = 415;

static char g_codebuf[] = {0xd1,0xf2,0x0d,0x0a,0xff,0xcd,0x51,0x55,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x05,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x73,0x4f,0x00,0x00,0x00,0x64,0x00,

0x00,0x5a,0x00,0x00,0x64,0x01,0x00,0x64,0x02,0x00,0x6b,0x01,0x00,0x5a,0x01,0x00,

0x64,0x01,0x00,0x64,0x02,0x00,0x6b,0x02,0x00,0x5a,0x02,0x00,0x64,0x01,0x00,0x64,

0x02,0x00,0x6b,0x03,0x00,0x5a,0x03,0x00,0x64,0x03,0x00,0x84,0x00,0x00,0x5a,0x04,

0x00,0x65,0x05,0x00,0x64,0x04,0x00,0x6a,0x02,0x00,0x6f,0x0b,0x00,0x01,0x65,0x04,

0x00,0x83,0x00,0x00,0x01,0x6e,0x01,0x00,0x01,0x64,0x02,0x00,0x53,0x28,0x05,0x00,

0x00,0x00,0x73,0x2b,0x00,0x00,0x00,0x0a,0x43,0x72,0x65,0x61,0x74,0x65,0x64,0x20,

0x6f,0x6e,0x20,0x32,0x30,0x31,0x35,0x2d,0x30,0x34,0x2d,0x31,0x34,0x0a,0x0a,0x40,

0x61,0x75,0x74,0x68,0x6f,0x72,0x3a,0x20,0x74,0x61,0x6e,0x67,0x63,0x68,0x65,0x6e,

0x67,0x0a,0x69,0xff,0xff,0xff,0xff,0x4e,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x01,0x00,0x00,0x00,0x43,0x00,0x00,0x00,0x73,0x09,0x00,0x00,0x00,0x64,0x01,

0x00,0x47,0x48,0x64,0x00,0x00,0x53,0x28,0x02,0x00,0x00,0x00,0x4e,0x73,0x0b,0x00,

0x00,0x00,0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,0x6c,0x64,0x28,0x00,0x00,

0x00,0x00,0x28,0x00,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x00,0x28,0x00,0x00,0x00,

0x00,0x73,0x09,0x00,0x00,0x00,0x74,0x65,0x73,0x74,0x30,0x31,0x2e,0x70,0x79,0x74,

0x04,0x00,0x00,0x00,0x6d,0x61,0x69,0x6e,0x0e,0x00,0x00,0x00,0x73,0x02,0x00,0x00,

0x00,0x00,0x01,0x74,0x08,0x00,0x00,0x00,0x5f,0x5f,0x6d,0x61,0x69,0x6e,0x5f,0x5f,

0x28,0x06,0x00,0x00,0x00,0x74,0x07,0x00,0x00,0x00,0x5f,0x5f,0x64,0x6f,0x63,0x5f,

0x5f,0x74,0x03,0x00,0x00,0x00,0x73,0x79,0x73,0x74,0x02,0x00,0x00,0x00,0x6f,0x73,

0x74,0x09,0x00,0x00,0x00,0x74,0x72,0x61,0x63,0x65,0x62,0x61,0x63,0x6b,0x52,0x00,

0x00,0x00,0x00,0x74,0x08,0x00,0x00,0x00,0x5f,0x5f,0x6e,0x61,0x6d,0x65,0x5f,0x5f,

0x28,0x00,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x00,0x73,

0x09,0x00,0x00,0x00,0x74,0x65,0x73,0x74,0x30,0x31,0x2e,0x70,0x79,0x74,0x08,0x00,

0x00,0x00,0x3c,0x6d,0x6f,0x64,0x75,0x6c,0x65,0x3e,0x08,0x00,0x00,0x00,0x73,0x0c,

0x00,0x00,0x00,0x06,0x02,0x0c,0x01,0x0c,0x01,0x0c,0x02,0x09,0x03,0x0d,0x01};

int main(int argc, char *argv[])

{

int file_size;

PyCodeObject *pycode;

PyObject *mainmodule, *maindict;

Py_Initialize();

PySys_SetArgv(argc, argv);

mainmodule = PyImport_AddModule("__main__");

maindict = PyModule_GetDict(mainmodule);

pycode = (PyCodeObject *)PyMarshal_ReadObjectFromString(g_codebuf+8, g_file_size-8);

PyEval_EvalCode(pycode, maindict, maindict);

PyErr_Clear();

Py_Finalize();

return 0;

}

其中test01.c源码中全局变量g_codebuf是test01.pyc的内容。

3. 工具的下载地址

[FATAL] [INS-10105] The given response file /usr/local/products/oracle19c/install/response/db_install.rsp is not valid. CAUSE: Syntactically incorrect response file. Either unexpected variables are specified or expected variables are not specified in the response file. ACTION: Refer the latest product specific response file template SUMMARY: - cvc-complex-type.2.4.a: Invalid content was found starting with element 'oracle.install.db.OSKMDBA'. One of '{oracle.install.responseFileVersion, oracle.install.option, oracle.install.db.ConfigureAsContainerDB, SELECTED_LANGUAGES, ORACLE_HOSTNAME, INVENTORY_LOCATION, UNIX_GROUP_NAME, oracle.install.db.InstallEdition, ORACLE_HOME, OSDBA_GROUP, OSOPER_GROUP, OSBACKUPDBA_GROUP, OSDGDBA_GROUP, OSKMDBA_GROUP, OSRACDBA_GROUP, CLUSTER_NODES, oracle.install.db.rac.configurationType, oracle.install.db.rac.serverpoolName, oracle.install.db.rac.serverpoolCardinality, oracle.install.db.CLUSTER_NODES, oracle.install.db.isRACOneInstall, oracle.install.db.racOneServiceName, oracle.install.db.DBA_GROUP, oracle.install.db.OPER_GROUP, oracle.install.db.BACKUPDBA_GROUP, oracle.install.db.DGDBA_GROUP, oracle.install.db.KMDBA_GROUP, oracle.install.db.OSDBA_GROUP, oracle.install.db.OSOPER_GROUP, oracle.install.db.OSDGDBA_GROUP, oracle.install.db.OSKMDBA_GROUP, oracle.install.db.OSRACDBA_GROUP, oracle.install.IsBuiltInAccount, oracle.install.IsVirtualAccount, oracle.install.OracleHomeUserName, oracle.install.OracleHomeUserPassword, oracle.install.db.rootconfig.executeRootScript, oracle.install.db.rootconfig.configMethod, oracle.install.db.rootconfig.sudoPath, oracle.install.db.config.starterdb.type, oracle.install.db.config.starterdb.SID, oracle.install.db.config.starterdb.globalDBName, oracle.install.db.config.PDBName, oracle.install.db.config.pdbName, oracle.install.db.config.starterdb.characterSet, oracle.install.db.config.starterdb.memoryLimit, oracle.install.db.config.starterdb.storageType, oracle.install.db.config.starterdb.fileSystemStorage.dataLocation, oracle.install.db.config.starterdb.password.ALL, oracle.install.db.config.starterdb.password.SYS, oracle.install.db.config.starterdb.password.DBSNMP, oracle.install.db.config.starterdb.password.SYSTEM, oracle.install.db.config.starterdb.password.PDBADMIN, oracle.install.db.config.starterdb.managementOption, oracle.install.db.config.starterdb.omsHost, oracle.install.db.config.starterdb.omsPort, oracle.install.db.config.starterdb.emAdminUser, oracle.install.db.config.starterdb.emAdminPassword, oracle.install.db.config.starterdb.useWalletForPasswords, oracle.install.db.config.starterdb.walletLocation, oracle.install.db.config.starterdb.walletPassword, SECURITY_UPDATES_VIA_MYORACLESUPPORT, DECLINE_SECURITY_UPDATES, MYORACLESUPPORT_USERNAME, MYORACLESUPPORT_PASSWORD, PROXY_PORT, PROXY_HOST, PROXY_USER, PROXY_PWD, PROXY_REALM, COLLECTOR_SUPPORTHUB_URL, AUTOUPDATES_MYORACLESUPPORT_USERNAME, AUTOUPDATES_MYORACLESUPPORT_PASSWORD, oracle.installer.autoupdates.option, oracle.installer.autoupdates.downloadUpdatesLoc}' is expected.
08-23
[INS-10105] The given response file /usr/local/products/oracle19c/install/response/db_install.rsp is not valid. CAUSE: Syntactically incorrect response file. Either unexpected variables are specified or expected variables are not specified in the response file. ACTION: Refer the latest product specific response file template SUMMARY: - cvc-complex-type.2.4.a: Invalid content was found starting with element 'oracle.install.db.installOption'. One of '{oracle.install.option, SELECTED_LANGUAGES, ORACLE_HOSTNAME, INVENTORY_LOCATION, UNIX_GROUP_NAME, oracle.install.db.InstallEdition, ORACLE_HOME, OSDBA_GROUP, OSOPER_GROUP, OSBACKUPDBA_GROUP, OSDGDBA_GROUP, OSKMDBA_GROUP, OSRACDBA_GROUP, CLUSTER_NODES, oracle.install.db.rac.configurationType, oracle.install.db.rac.serverpoolName, oracle.install.db.rac.serverpoolCardinality, oracle.install.db.CLUSTER_NODES, oracle.install.db.isRACOneInstall, oracle.install.db.racOneServiceName, oracle.install.db.DBA_GROUP, oracle.install.db.OPER_GROUP, oracle.install.db.BACKUPDBA_GROUP, oracle.install.db.DGDBA_GROUP, oracle.install.db.KMDBA_GROUP, oracle.install.db.OSDBA_GROUP, oracle.install.db.OSOPER_GROUP, oracle.install.db.OSKMDBA_GROUP, oracle.install.IsBuiltInAccount, oracle.install.IsVirtualAccount, oracle.install.OracleHomeUserName, oracle.install.OracleHomeUserPassword, oracle.install.db.rootconfig.executeRootScript, oracle.install.db.rootconfig.configMethod, oracle.install.db.config.starterdb.SID, oracle.install.db.config.PDBName, oracle.install.db.config.pdbName, oracle.install.db.config.starterdb.characterSet, oracle.install.db.config.starterdb.memoryLimit, oracle.install.db.config.starterdb.password.SYS, oracle.install.db.config.starterdb.password.SYSTEM, oracle.install.db.config.starterdb.password.PDBADMIN, oracle.install.db.config.starterdb.managementOption, oracle.install.db.config.starterdb.omsPort, oracle.install.db.config.starterdb.emAdminPassword, oracle.install.db.config.starterdb.useWalletForPasswords, oracle.install.db.config.starterdb.walletLocation, oracle.install.db.config.starterdb.walletPassword, SECURITY_UPDATES_VIA_MYORACLESUPPORT, DECLINE_SECURITY_UPDATES, MYORACLESUPPORT_USERNAME, MYORACLESUPPORT_PASSWORD, PROXY_PORT, PROXY_HOST, PROXY_USER, PROXY_PWD, PROXY_REALM, COLLECTOR_SUPPORTHUB_URL, AUTOUPDATES_MYORACLESUPPORT_USERNAME, AUTOUPDATES_MYORACLESUPPORT_PASSWORD, oracle.installer.autoupdates.option, oracle.installer.autoupdates.downloadUpdatesLoc}' is expected.
08-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值