导入numpy后无法将Python脚本打包为.exe的问题求助
Alright, let’s dig into this WinError 193 issue you’re facing when trying to package your QA Lot Tracking app with PyInstaller after adding numpy. That error almost always points to an architecture mismatch or a problem with how PyInstaller handles numpy’s binary dependencies. Here’s how to resolve it step by step:
1. Verify Architecture Mismatch Between Python and Numpy
WinError 193 is super common when you mix 32-bit and 64-bit components. Let’s check:
- First, confirm your Python’s architecture:
python -c "import sys; print('64-bit' if sys.maxsize > 2**32 else '32-bit')" - Then check numpy’s architecture by looking at its install path:
If the path includespython -c "import numpy; print(numpy.__file__)"amd64, it’s 64-bit; if it haswin32, it’s 32-bit. If they don’t match:- Uninstall numpy:
pip uninstall numpy - Reinstall the correct version (your pip will automatically match your Python’s architecture):
pip install numpy
- Uninstall numpy:
2. Update PyInstaller and Numpy to Compatible Versions
Your log shows you’re using PyInstaller 3.5, which is quite outdated (released in 2019). Older versions struggle with newer numpy releases. Let’s update:
- Upgrade PyInstaller:
pip install --upgrade pyinstaller - Update numpy to a version compatible with your Python 3.7:
pip install --upgrade numpy
Try repackaging after these updates—this alone fixes most numpy-related PyInstaller issues.
3. Use a Virtual Environment to Eliminate Dependency Conflicts
Your log also shows a warning about missing greenlet for gevent, which hints at potential system-wide dependency conflicts. A clean virtual environment will isolate your app’s dependencies:
- Create and activate a venv:
python -m venv qa_app_env qa_app_env\Scripts\activate - Install all your app’s dependencies from scratch:
pip install numpy pyinstaller [add any other libraries your app uses, like gevent if needed] - Run PyInstaller from within the activated venv:
pyinstaller -y "C:/Users/adamt/Desktop/QA Lot Tracking Application/QAInternalTrackingApp_v5.py"
4. Manually Tweak the PyInstaller Spec File
If the above steps don’t work, you can force PyInstaller to include numpy’s hidden dependencies:
- After running PyInstaller once (even if it fails), you’ll get a
.specfile (look in your temp directory or project folder). Open it in a text editor. - Add numpy-related imports to the
hiddenimportslist in theAnalysissection:a = Analysis( ['QAInternalTrackingApp_v5.py'], pathex=['C:\\Users\\adamt\\Desktop\\QA Lot Tracking Application'], binaries=[], datas=[], hiddenimports=['numpy.core._dtype_ctypes', 'numpy'], hookspath=[], runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=None, noarchive=False ) - Repackage using the spec file:
pyinstaller -y QAInternalTrackingApp_v5.spec
5. Fix a Corrupted Numpy Installation
Sometimes numpy’s binary files get corrupted during installation. Let’s do a clean reinstall:
- Uninstall numpy completely:
pip uninstall -y numpy - Manually delete any leftover numpy folders in your site-packages (check
C:\Users\adamt\AppData\Roaming\Python\Python37\site-packagesfornumpyandnumpy.dist-infofolders) - Reinstall numpy:
pip install numpy
Start with the first two steps—they’re the quickest fixes for this specific error. If those don’t work, move to the virtual environment approach, as it eliminates most dependency-related headaches.
内容的提问来源于stack exchange,提问作者Adamter

