使用PyInstaller打包Tkinter项目时遭遇sysconfig隐藏导入错误
It looks like you're hitting a compatibility snag between PyInstaller 3.3.1 and Python 3.6 on macOS. This error pops up because older PyInstaller versions don’t properly handle the dynamic sysconfig data module that Python 3.6 uses on Darwin systems. Here are a couple of straightforward fixes:
Solution 1: Upgrade PyInstaller (Recommended)
The simplest and most reliable fix is to update PyInstaller to a version that fully supports Python 3.6. Run this command in your terminal:
pip install --upgrade pyinstaller
Once upgraded, re-run your original PyInstaller command. The newer version will automatically handle the sysconfig dependency without needing manual hidden imports.
Solution 2: Manually Add the Missing Hidden Import
If upgrading isn’t an option for you, you can explicitly specify the correct sysconfig data module as a hidden import:
- First, find the exact name of the sysconfig data module your Python installation uses. Run this command:
python -c "import sysconfig; print(sysconfig._get_sysconfigdata_name())"
This will output something like _sysconfigdata_m_darwin_darwin.
2. Add this module to your PyInstaller command. For example, if the output was _sysconfigdata_m_darwin_darwin, your command becomes:
pyinstaller --clean --hidden-import tkinter --hidden-import _sysconfigdata_m_darwin_darwin test.py
Alternatively, if you’re using the generated test.spec file, edit the hiddenimports list to include this module name:
hiddenimports=['tkinter', '_sysconfigdata_m_darwin_darwin'],
Then build using the spec file:
pyinstaller --clean test.spec
Quick Check
Double-check that you’re using the same Python environment where PyInstaller is installed to avoid path mismatches. For tkinter, it should be included by default with Python 3.6 on macOS, but if you’re using a custom build, verify it’s properly installed.
内容的提问来源于stack exchange,提问作者tatiaris

