python 使用pyinstaller打包python为exe文件

本文介绍使用 PyInstaller 将 Python 脚本打包成独立可执行文件的过程。包括配置选项、构建流程及命令行参数解析等内容。

打包步骤:

sudo su gpadmin
cd /home/gpadmin/zack/pyinstaller-2.0
rm -rf callemc
python pyinstaller.py --onefile  ./gaochao/callemctest/callemc.py
python pyinstaller.py ./callemc/callemc.spec


#! /usr/bin/env python

#
# Wrapper around Configure.py / Makespec.py / Build.py
#
# Copyright (C) 2010, Martin Zibricky
# Copyright (C) 2011, Hartmut Goebel
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

import os
import optparse

import PyInstaller.makespec
import PyInstaller.build
import PyInstaller.compat
import PyInstaller.log


# Warn when old command line option is used

from PyInstaller import get_version
from PyInstaller.log import logger


def run_makespec(opts, args):
    # Split pathex by using the path separator
    temppaths = opts.pathex[:]
    opts.pathex = []
    for p in temppaths:
        opts.pathex.extend(p.split(os.pathsep))

    spec_file = PyInstaller.makespec.main(args, **opts.__dict__)
    logger.info('wrote %s' % spec_file)
    return spec_file


def run_build(opts, spec_file):
    PyInstaller.build.main(spec_file, **opts.__dict__)


def __add_options(parser):
    parser.add_option('-v', '--version', default=False, action='store_true',
                      help='show program version')


def main():
    parser = optparse.OptionParser(
        usage='python %prog [opts] <scriptname> [ <scriptname> ...] | <specfile>'
        )
    __add_options(parser)
    PyInstaller.makespec.__add_options(parser)
    PyInstaller.build.__add_options(parser)
    PyInstaller.log.__add_options(parser)
    PyInstaller.compat.__add_obsolete_options(parser)

    opts, args = parser.parse_args()
    PyInstaller.log.__process_options(parser, opts)

    # Print program version and exit
    if opts.version:
        print get_version()
        raise SystemExit(0)

    if not args:
        parser.error('Requires at least one scriptname file '
                     'or exactly one .spec-file')

    # Skip creating .spec when .spec file is supplied
    if args[0].endswith('.spec'):
        spec_file = args[0]
    else:
        spec_file = run_makespec(opts, args)

    run_build(opts, spec_file)


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        raise SystemExit("Aborted by user request.")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值