python 调用含参数的exe

示例:

1. 生成exe的Python文件,用pyinstaller -F exe_with_args.py打包成exe:
# -*- coding: utf-8 -*-
# exe_with_args.py

def exe_args():
    import sys
    if len(sys.argv):
        print('[1]Have args.')
        print(sys.argv)
    else:
        print('[1]No args.')


def exe_args2():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--json', required=True)
    args = parser.parse_args()
    if args:
        print('[2]Have args.')
        print(args.json)
        # print(args)
    else:
        print('[2]No args.')


def main():
    # exe_args()
    exe_args2()


if __name__ == '__main__':
    main()
    input('Press any key to exit...')

2. 调用exe示例:
# -*- coding: utf-8 -*-
# call_exe.py


def call_exe():
    import os
    print('Call exe begin.')
    # exe_path = os.path.join(os.getcwd(), 'dist', 'exe_with_args.exe')
    # os.system(exe_path+' 1 2 3')
    os.system(r' D:\Project\dist\exe_with_args.exe --json "{"a":"1"}" ')
    print('Call exe finished.')


def main():
    call_exe()


if __name__ == '__main__':
    main()

你可能感兴趣的:(Python)