How to Fix Python py2exe Build Error
In this tutorial, you'll learn about How to Fix Python py2exe Build Error. We cover key concepts, practical examples, and best practices.
Py2exe fails to build Windows executables when the build script is missing required options, MSVCR90.dll is not included, or the Python installation is not for Windows.
The Wrong Way
# setup.py
from distutils.core import setup
import py2exe
setup(
windows=[{"script": "myscript.py"}],
)
Building:
python setup.py py2exe
Output:
error: MSVCP90.dll: No such file or directory
The Right Way
# setup.py
from distutils.core import setup
import py2exe
setup(
windows=[{"script": "myscript.py"}],
options={
"py2exe": {
"includes": ["os", "sys", "json"],
"excludes": ["tkinter"],
"dll_excludes": ["MSVCP90.dll"],
"bundle_files": 1,
"compressed": True,
}
},
zipfile=None,
)
Build:
python setup.py py2exe
The executable is created in the dist folder.
Prevention Tips
- Use py2exe only on Windows. It does not work on Linux or macOS.
- Use
dll_excludesto skip missing DLLs. - Set
bundle_files=1for a single executable. - Set
compressed=Trueto reduce file size. - Test the executable on a Windows machine without Python.
Common Mistakes with py2exe
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro