You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将pyw文件转换为exe?Python3下PyInstaller与py2exe实现方法

Converting .pyw to .exe with PyInstaller (Python 3)

Hey there! Let's break down how to turn your .pyw script into a standalone .exe file using PyInstaller first—it’s the most popular tool for this and works seamlessly with Python 3. If you run into any hiccups with it, I’ll also walk you through using py2exe as an alternative.

Using PyInstaller

PyInstaller handles .pyw files (which run without a console window) natively, making it a great first choice. Here's how to use it:

  • First, install PyInstaller via pip:
    pip install pyinstaller
    
  • Navigate to your script's folder in the command line, then run this command—the --windowed flag is key because it matches the no-console behavior of .pyw files:
    pyinstaller --windowed your_script.pyw
    
  • If you want a single, self-contained .exe instead of a folder with dependencies, add the --onefile flag:
    pyinstaller --windowed --onefile your_script.pyw
    
  • Once the process finishes, your .exe will be waiting in the dist subfolder of your script's directory.

If PyInstaller Doesn't Work: Using py2exe

Py2exe is another reliable option for converting Python scripts to executables. Here's the step-by-step process tailored for .pyw files:

  • First, install py2exe:
    pip install py2exe
    
  • Create a setup.py file in the same folder as your .pyw script. This file tells py2exe how to build your executable. Use this template:
    from distutils.core import setup
    import py2exe
    
    setup(
        # Use `windows` instead of `console` to skip the console window
        windows=['your_script.pyw'],
        options={
            'py2exe': {
                # Optional: Bundle all files into a single exe
                'bundle_files': 1,
                # Compress the executable to save space
                'compressed': True
            }
        },
        # Required if using `bundle_files=1` to avoid a separate zip file
        zipfile=None
    )
    
  • Run the setup script from the command line:
    python setup.py py2exe
    
  • Your finished .exe will be in the dist folder created during the build process.

内容的提问来源于stack exchange,提问作者Costel biju

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:59:44