There are several ways to compile Python code to an executable (.exe) file. Here are the most popular methods:
PyInstaller is the most widely used tool and works well for most projects:
# Install
pip install pyinstaller
# Basic usage - creates a folder with dependencies
pyinstaller your_script.py
# Create a single .exe file
pyinstaller --onefile your_script.py
# Add an icon
pyinstaller --onefile --icon=myicon.ico your_script.py
# Hide the console window (for GUI apps)
pyinstaller --onefile --noconsole your_script.pyIf you prefer a graphical interface:
pip install auto-py-to-exe
auto-py-to-exeThis opens a browser-based GUI where you can configure all options visually.
Another solid option that's cross-platform:
pip install cx_Freeze
# Create a setup.py file, then run:
python setup.py buildCompiles Python to C++ first, resulting in faster executables:
pip install nuitka
python -m nuitka --onefile your_script.py- The .exe will only work on Windows (for cross-platform, look into PyInstaller for each OS)
- The resulting file can be large (includes Python interpreter + dependencies)
- Antivirus software sometimes flags these files as suspicious
- Test thoroughly - not all Python packages work perfectly when compiled
PyInstaller with --onefile is usually the best starting point for most projects. Would you like help with a specific tool or running into any particular issues?