PyInstaller打包Python为exe后跨机运行报错:no module named encodings
Hey, I've run into this exact issue before—even though your code doesn't use any third-party libraries, the encodings module is a core part of Python's standard library that handles character encoding (like your decode("ASCII") call), and sometimes PyInstaller's auto-dependency analyzer misses it when building a single-file executable. Here are a few tried-and-true fixes:
Force include the encodings module with a hidden import
Add the--hidden-import=encodingsflag to your PyInstaller command to explicitly tell it to package this module:pyinstaller --onefile --hidden-import=encodings <filename>.pyThis works because PyInstaller doesn't always pick up implicit dependencies from standard library modules.
Match your Python environment to the target machine
Make sure the Python version you used to build the exe matches the target system's architecture (32-bit vs 64-bit). A 64-bit exe won't run properly on a 32-bit Windows machine, and this can manifest as weird missing module errors. Stick to stable Python versions (3.8–3.11 tend to be most reliable for PyInstaller).Collect all encodings-related files
If the hidden import alone doesn't work, use the--collect-allflag to grab every file associated with the encodings module. This will make your exe a bit larger, but it eliminates any chance of missing components:pyinstaller --onefile --collect-all encodings <filename>.pyCheck for missing VC++ Redistributable
Sometimes this error is a red herring—Python's core modules depend on the Visual C++ Redistributable package. If your target machine doesn't have the version matching your Python installation (e.g., Python 3.8+ needs VC++ 2019), install that first and see if the error goes away.
Your code's bytes.fromhex(data).decode("ASCII") line directly relies on the encodings module, so making sure it's included in the package is key to fixing this.
内容的提问来源于stack exchange,提问作者taherafahimi

