232 lines
7.5 KiB
Python
232 lines
7.5 KiB
Python
|
|
"""
|
|||
|
|
编译环境检查工具
|
|||
|
|
用于诊断 PyInstaller 编译问题
|
|||
|
|
作者:太一
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import sys
|
|||
|
|
import subprocess
|
|||
|
|
import os
|
|||
|
|
|
|||
|
|
def check_python():
|
|||
|
|
"""检查Python版本"""
|
|||
|
|
print("\n" + "="*60)
|
|||
|
|
print("【1】检查 Python 环境")
|
|||
|
|
print("="*60)
|
|||
|
|
version = sys.version
|
|||
|
|
print(f"✓ Python 版本: {version}")
|
|||
|
|
print(f"✓ Python 路径: {sys.executable}")
|
|||
|
|
|
|||
|
|
if sys.version_info < (3, 7):
|
|||
|
|
print("⚠️ 警告: Python 版本过低,建议使用 Python 3.7+")
|
|||
|
|
return False
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
def check_pip():
|
|||
|
|
"""检查pip"""
|
|||
|
|
print("\n" + "="*60)
|
|||
|
|
print("【2】检查 pip")
|
|||
|
|
print("="*60)
|
|||
|
|
try:
|
|||
|
|
result = subprocess.run([sys.executable, '-m', 'pip', '--version'],
|
|||
|
|
capture_output=True, text=True, timeout=10)
|
|||
|
|
if result.returncode == 0:
|
|||
|
|
print(f"✓ pip 版本: {result.stdout.strip()}")
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print("✗ pip 不可用")
|
|||
|
|
return False
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"✗ 检查 pip 失败: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def check_module(module_name):
|
|||
|
|
"""检查模块是否安装"""
|
|||
|
|
try:
|
|||
|
|
__import__(module_name)
|
|||
|
|
return True
|
|||
|
|
except ImportError:
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def check_dependencies():
|
|||
|
|
"""检查依赖项"""
|
|||
|
|
print("\n" + "="*60)
|
|||
|
|
print("【3】检查必需的依赖包")
|
|||
|
|
print("="*60)
|
|||
|
|
|
|||
|
|
required = {
|
|||
|
|
'pyinstaller': 'PyInstaller',
|
|||
|
|
'cryptography': 'cryptography',
|
|||
|
|
'requests': 'requests',
|
|||
|
|
'tkinter': 'tkinter',
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
all_ok = True
|
|||
|
|
for module, name in required.items():
|
|||
|
|
if check_module(module):
|
|||
|
|
try:
|
|||
|
|
if module == 'pyinstaller':
|
|||
|
|
import pyinstaller
|
|||
|
|
version = pyinstaller.__version__
|
|||
|
|
elif module == 'cryptography':
|
|||
|
|
import cryptography
|
|||
|
|
version = cryptography.__version__
|
|||
|
|
elif module == 'requests':
|
|||
|
|
import requests
|
|||
|
|
version = requests.__version__
|
|||
|
|
elif module == 'tkinter':
|
|||
|
|
import tkinter
|
|||
|
|
version = tkinter.TkVersion
|
|||
|
|
else:
|
|||
|
|
version = "已安装"
|
|||
|
|
print(f"✓ {name:20s} - 版本 {version}")
|
|||
|
|
except:
|
|||
|
|
print(f"✓ {name:20s} - 已安装")
|
|||
|
|
else:
|
|||
|
|
print(f"✗ {name:20s} - 未安装")
|
|||
|
|
all_ok = False
|
|||
|
|
|
|||
|
|
return all_ok
|
|||
|
|
|
|||
|
|
def check_pyinstaller():
|
|||
|
|
"""检查PyInstaller"""
|
|||
|
|
print("\n" + "="*60)
|
|||
|
|
print("【4】检查 PyInstaller")
|
|||
|
|
print("="*60)
|
|||
|
|
try:
|
|||
|
|
result = subprocess.run(['pyinstaller', '--version'],
|
|||
|
|
capture_output=True, text=True, timeout=10)
|
|||
|
|
if result.returncode == 0:
|
|||
|
|
version = result.stdout.strip()
|
|||
|
|
print(f"✓ PyInstaller 版本: {version}")
|
|||
|
|
|
|||
|
|
# 测试简单编译
|
|||
|
|
print("\n测试简单的编译...")
|
|||
|
|
test_py = "_test_compile.py"
|
|||
|
|
test_exe = "_test_compile.exe"
|
|||
|
|
|
|||
|
|
# 创建测试文件
|
|||
|
|
with open(test_py, 'w', encoding='utf-8') as f:
|
|||
|
|
f.write('print("Hello, PyInstaller!")\ninput("按回车键退出...")')
|
|||
|
|
|
|||
|
|
cmd = [
|
|||
|
|
'pyinstaller', '--onefile', '--clean', '--noconfirm',
|
|||
|
|
'--distpath', '.', '--workpath', '.build_test',
|
|||
|
|
'--specpath', '.build_test', '--noupx',
|
|||
|
|
test_py
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
print(f"执行命令: {' '.join(cmd)}")
|
|||
|
|
result = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
|
|||
|
|
|
|||
|
|
if result.returncode == 0 and os.path.exists(test_exe):
|
|||
|
|
print(f"✓ 编译测试成功!生成文件: {test_exe}")
|
|||
|
|
print(f" 文件大小: {os.path.getsize(test_exe) / 1024 / 1024:.2f} MB")
|
|||
|
|
|
|||
|
|
# 清理测试文件
|
|||
|
|
try:
|
|||
|
|
os.remove(test_py)
|
|||
|
|
os.remove(test_exe)
|
|||
|
|
import shutil
|
|||
|
|
if os.path.exists('.build_test'):
|
|||
|
|
shutil.rmtree('.build_test')
|
|||
|
|
if os.path.exists('_test_compile.spec'):
|
|||
|
|
os.remove('_test_compile.spec')
|
|||
|
|
print(" 测试文件已清理")
|
|||
|
|
except:
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print("✗ 编译测试失败")
|
|||
|
|
if result.stderr:
|
|||
|
|
print("\n错误信息:")
|
|||
|
|
print(result.stderr[-1000:])
|
|||
|
|
return False
|
|||
|
|
else:
|
|||
|
|
print("✗ PyInstaller 不可用")
|
|||
|
|
print("请运行: pip install pyinstaller")
|
|||
|
|
return False
|
|||
|
|
except FileNotFoundError:
|
|||
|
|
print("✗ PyInstaller 未安装或不在系统路径中")
|
|||
|
|
print("请运行: pip install pyinstaller")
|
|||
|
|
return False
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"✗ 检查 PyInstaller 失败: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def check_disk_space():
|
|||
|
|
"""检查磁盘空间"""
|
|||
|
|
print("\n" + "="*60)
|
|||
|
|
print("【5】检查磁盘空间")
|
|||
|
|
print("="*60)
|
|||
|
|
try:
|
|||
|
|
import shutil
|
|||
|
|
total, used, free = shutil.disk_usage(os.getcwd())
|
|||
|
|
|
|||
|
|
print(f"当前目录: {os.getcwd()}")
|
|||
|
|
print(f"总空间: {total / (1024**3):.2f} GB")
|
|||
|
|
print(f"已使用: {used / (1024**3):.2f} GB")
|
|||
|
|
print(f"可用: {free / (1024**3):.2f} GB")
|
|||
|
|
|
|||
|
|
if free < 1 * 1024**3: # 小于1GB
|
|||
|
|
print("⚠️ 警告: 可用空间不足 1GB,编译可能失败")
|
|||
|
|
return False
|
|||
|
|
else:
|
|||
|
|
print("✓ 磁盘空间充足")
|
|||
|
|
return True
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"⚠️ 无法检查磁盘空间: {e}")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
"""主函数"""
|
|||
|
|
print("\n" + "█"*60)
|
|||
|
|
print("█" + " "*58 + "█")
|
|||
|
|
print("█" + " 编译环境诊断工具 - EXE Encryption Tool ".center(58) + "█")
|
|||
|
|
print("█" + " "*58 + "█")
|
|||
|
|
print("█"*60)
|
|||
|
|
|
|||
|
|
results = []
|
|||
|
|
|
|||
|
|
results.append(("Python", check_python()))
|
|||
|
|
results.append(("pip", check_pip()))
|
|||
|
|
results.append(("依赖包", check_dependencies()))
|
|||
|
|
results.append(("PyInstaller", check_pyinstaller()))
|
|||
|
|
results.append(("磁盘空间", check_disk_space()))
|
|||
|
|
|
|||
|
|
print("\n" + "="*60)
|
|||
|
|
print("【检查结果汇总】")
|
|||
|
|
print("="*60)
|
|||
|
|
|
|||
|
|
all_passed = True
|
|||
|
|
for name, passed in results:
|
|||
|
|
status = "✓ 通过" if passed else "✗ 失败"
|
|||
|
|
print(f"{name:20s} {status}")
|
|||
|
|
if not passed:
|
|||
|
|
all_passed = False
|
|||
|
|
|
|||
|
|
print("="*60)
|
|||
|
|
|
|||
|
|
if all_passed:
|
|||
|
|
print("\n🎉 所有检查通过!编译环境正常。")
|
|||
|
|
print("\n如果仍然遇到编译问题,请检查:")
|
|||
|
|
print(" 1. 杀毒软件是否拦截")
|
|||
|
|
print(" 2. 防火墙设置")
|
|||
|
|
print(" 3. 用户权限(建议以管理员身份运行)")
|
|||
|
|
print(" 4. 临时文件夹权限")
|
|||
|
|
else:
|
|||
|
|
print("\n❌ 部分检查失败,请根据上述提示修复问题。")
|
|||
|
|
print("\n常见解决方案:")
|
|||
|
|
print(" 1. 安装缺失的包: pip install -r requirements.txt")
|
|||
|
|
print(" 2. 升级 PyInstaller: pip install --upgrade pyinstaller")
|
|||
|
|
print(" 3. 清理 pip 缓存: pip cache purge")
|
|||
|
|
print(" 4. 重新安装依赖: pip uninstall pyinstaller && pip install pyinstaller")
|
|||
|
|
|
|||
|
|
print("\n")
|
|||
|
|
input("按回车键退出...")
|
|||
|
|
|
|||
|
|
if __name__ == '__main__':
|
|||
|
|
main()
|
|||
|
|
|