Pycharm怎么打包Python脚本?

 品味a江湖_232_466 发布于 2022-11-06 16:12

求助,目的是想将Python脚本放到其它WindowsPC上运行,该PC不需要安装解释器,目前能想到的办法就是打包成exe,但不会用。而我现在用的工具是Pycharm,所以请教下,怎么使用Pycharm将Python进行打包。

2 个回答
  • 用cx_Freeze即可,它和py2exe,py2app类似,不过是跨平台的,并且支持python3。 例子:

    import sys
    from cx_Freeze import setup, Executable
    
    # Dependencies are automatically detected, but it might need fine tuning.
    build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
    
    # GUI applications require a different base on Windows (the default is for a
    # console application).
    base = None
    if sys.platform == "win32":
        base = "Win32GUI"
    
    setup(  name = "guifoo",
            version = "0.1",
            description = "My GUI application!",
            options = {"build_exe": build_exe_options},
            executables = [Executable("guifoo.py", base=base)])
    

    之后:

    python setup.py build
    

    即可

    2022-11-12 01:48 回答
  • pycharm不知道,一般打包的话我用py2exe的
    具体下载地址google一下,装好后在项目下建立setup.py,大致内容如下,N多参数可以看DOC

    from distutils.core import setup
    import py2exe
    import sys
    sys.argv.append('py2exe')
    includes=["email.*"]
    datafiles = [("etc",["etc/serverBash.conf"]),("bin",["bin/7z.exe","bin/7z.dll"]),("var",[])]
    #,"bundle_files":1  这边注意,64位系统不支持参数1,得用3,然后会生成一堆lib,不过建议打包的时候在32位系统上。
    opti = {"py2exe":{"compressed":1,"optimize":2,"bundle_files":1,"includes":includes}}
    setup(
    version = "0.1.0", 
    description = "Mysql Dump  Bash",
    name = "Mysql Dump  Bash", 
    console = [{"script":'ServerBash.py'}],
    options=opti,
    zipfile=None,
    data_files=datafiles,
    )
    

    然后在命令行下运行: >>python setup.py py2exe 就会在项目生成dist目录,里面就是你要的文件

    2022-11-12 01:48 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有