fix
This commit is contained in:
66
scripts/demo_python_env_manager.bat
Normal file
66
scripts/demo_python_env_manager.bat
Normal file
@@ -0,0 +1,66 @@
|
||||
@echo off
|
||||
REM Python环境管理器演示脚本
|
||||
|
||||
echo 🐍 Python Environment Manager Demo
|
||||
echo =====================================
|
||||
echo.
|
||||
|
||||
echo 这个演示将展示Python环境管理器的功能:
|
||||
echo.
|
||||
echo 1. 📊 查看Python环境状态
|
||||
echo 2. 📦 管理Python包
|
||||
echo 3. 🔧 设置嵌入式Python环境
|
||||
echo.
|
||||
|
||||
echo 📋 使用步骤:
|
||||
echo.
|
||||
echo 1. 启动应用:
|
||||
echo cargo tauri dev
|
||||
echo.
|
||||
echo 2. 访问Python环境管理页面:
|
||||
echo - 点击左侧导航栏的 "Python环境"
|
||||
echo - 或访问 /python-env-manager 路由
|
||||
echo.
|
||||
echo 3. 查看环境状态:
|
||||
echo - 绿色:环境可用
|
||||
echo - 红色:环境不可用或未设置
|
||||
echo.
|
||||
echo 4. 设置嵌入式Python(如果需要):
|
||||
echo - 点击 "设置嵌入式Python" 按钮
|
||||
echo - 等待自动下载和配置
|
||||
echo.
|
||||
echo 5. 管理Python包:
|
||||
echo - 点击 "安装包" 添加新包
|
||||
echo - 使用 "升级" 更新现有包
|
||||
echo - 使用 "卸载" 移除不需要的包
|
||||
echo.
|
||||
echo 6. 搜索包:
|
||||
echo - 使用搜索框快速查找已安装的包
|
||||
echo.
|
||||
|
||||
echo 🎯 推荐测试包:
|
||||
echo.
|
||||
echo - requests (HTTP请求库)
|
||||
echo - Pillow (图像处理库)
|
||||
echo - certifi (SSL证书库)
|
||||
echo - numpy (数值计算库,可选)
|
||||
echo - pandas (数据分析库,可选)
|
||||
echo.
|
||||
|
||||
echo 🔧 故障排除:
|
||||
echo.
|
||||
echo 如果遇到问题:
|
||||
echo 1. 检查网络连接
|
||||
echo 2. 确保有足够磁盘空间
|
||||
echo 3. 查看控制台错误信息
|
||||
echo 4. 点击 "刷新" 重新加载状态
|
||||
echo.
|
||||
|
||||
echo ✨ 现在启动应用来体验Python环境管理器!
|
||||
echo.
|
||||
|
||||
pause
|
||||
|
||||
echo 🚀 启动Tauri应用...
|
||||
cd /d "%~dp0.."
|
||||
cargo tauri dev
|
||||
40
scripts/setup_embedded_python.bat
Normal file
40
scripts/setup_embedded_python.bat
Normal file
@@ -0,0 +1,40 @@
|
||||
@echo off
|
||||
REM 设置嵌入式Python环境
|
||||
|
||||
echo 🐍 Setting up embedded Python for MixVideo V2...
|
||||
echo.
|
||||
|
||||
REM 检查Python是否可用
|
||||
python --version >nul 2>&1
|
||||
if errorlevel 1 (
|
||||
echo ❌ Python is not installed or not in PATH
|
||||
echo Please install Python 3.8+ and add it to PATH
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo ✅ Python environment check passed
|
||||
|
||||
REM 切换到项目根目录
|
||||
cd /d "%~dp0.."
|
||||
|
||||
REM 运行Python设置脚本
|
||||
echo 📦 Running embedded Python setup...
|
||||
python scripts\setup_embedded_python.py
|
||||
|
||||
if errorlevel 1 (
|
||||
echo ❌ Setup failed
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo.
|
||||
echo ✅ Setup completed successfully!
|
||||
echo.
|
||||
echo 📋 You can now:
|
||||
echo 1. Test embedded Python: src-tauri\python-embed\python.exe --version
|
||||
echo 2. Build your app: cargo tauri build
|
||||
echo 3. The Python environment will be included in the final executable
|
||||
echo.
|
||||
|
||||
pause
|
||||
286
scripts/setup_embedded_python.py
Normal file
286
scripts/setup_embedded_python.py
Normal file
@@ -0,0 +1,286 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
设置嵌入式Python环境
|
||||
下载Python嵌入式版本并配置依赖
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import urllib.request
|
||||
import zipfile
|
||||
import subprocess
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
def get_project_root():
|
||||
"""获取项目根目录"""
|
||||
return Path(__file__).parent.parent
|
||||
|
||||
def download_embedded_python(project_root):
|
||||
"""下载Python嵌入式版本"""
|
||||
print("📦 Downloading Python embedded version...")
|
||||
|
||||
# Python 3.11.0 嵌入式版本
|
||||
python_url = "https://www.python.org/ftp/python/3.11.0/python-3.11.0-embed-amd64.zip"
|
||||
embed_dir = project_root / "src-tauri" / "python-embed"
|
||||
zip_file = embed_dir / "python-embed.zip"
|
||||
|
||||
# 创建目录
|
||||
embed_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 下载文件
|
||||
if not zip_file.exists():
|
||||
print(f"Downloading from {python_url}...")
|
||||
urllib.request.urlretrieve(python_url, zip_file)
|
||||
print("✅ Download completed")
|
||||
else:
|
||||
print("✅ Python embed zip already exists")
|
||||
|
||||
# 解压文件
|
||||
print("📂 Extracting Python...")
|
||||
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
||||
zip_ref.extractall(embed_dir)
|
||||
|
||||
# 删除zip文件
|
||||
zip_file.unlink()
|
||||
|
||||
return embed_dir
|
||||
|
||||
def setup_pip(embed_dir):
|
||||
"""设置pip"""
|
||||
print("🔧 Setting up pip...")
|
||||
|
||||
python_exe = embed_dir / "python.exe"
|
||||
|
||||
# 下载get-pip.py
|
||||
get_pip_url = "https://bootstrap.pypa.io/get-pip.py"
|
||||
get_pip_file = embed_dir / "get-pip.py"
|
||||
|
||||
if not get_pip_file.exists():
|
||||
print("Downloading get-pip.py...")
|
||||
urllib.request.urlretrieve(get_pip_url, get_pip_file)
|
||||
|
||||
# 修改python311._pth文件以启用site-packages
|
||||
pth_file = embed_dir / "python311._pth"
|
||||
if pth_file.exists():
|
||||
content = pth_file.read_text()
|
||||
if "#import site" in content:
|
||||
content = content.replace("#import site", "import site")
|
||||
pth_file.write_text(content)
|
||||
print("✅ Enabled site-packages")
|
||||
|
||||
# 安装pip
|
||||
try:
|
||||
result = subprocess.run([
|
||||
str(python_exe), str(get_pip_file)
|
||||
], capture_output=True, text=True, cwd=embed_dir)
|
||||
|
||||
if result.returncode == 0:
|
||||
print("✅ pip installed successfully")
|
||||
else:
|
||||
print(f"❌ pip installation failed: {result.stderr}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error installing pip: {e}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def install_dependencies(embed_dir):
|
||||
"""安装Python依赖"""
|
||||
print("📦 Installing Python dependencies...")
|
||||
|
||||
python_exe = embed_dir / "python.exe"
|
||||
|
||||
# 基础依赖
|
||||
dependencies = [
|
||||
"requests",
|
||||
"Pillow",
|
||||
"certifi",
|
||||
"urllib3",
|
||||
"charset-normalizer",
|
||||
"idna",
|
||||
# 如果需要其他依赖,在这里添加
|
||||
# "numpy",
|
||||
# "opencv-python-headless",
|
||||
# "pandas",
|
||||
]
|
||||
|
||||
for dep in dependencies:
|
||||
print(f"Installing {dep}...")
|
||||
try:
|
||||
result = subprocess.run([
|
||||
str(python_exe), "-m", "pip", "install", dep
|
||||
], capture_output=True, text=True, cwd=embed_dir)
|
||||
|
||||
if result.returncode == 0:
|
||||
print(f"✅ {dep} installed")
|
||||
else:
|
||||
print(f"❌ Failed to install {dep}: {result.stderr}")
|
||||
except Exception as e:
|
||||
print(f"❌ Error installing {dep}: {e}")
|
||||
|
||||
def copy_python_core(project_root, embed_dir):
|
||||
"""复制python_core模块到嵌入式Python"""
|
||||
print("📋 Copying python_core module...")
|
||||
|
||||
source_dir = project_root / "python_core"
|
||||
target_dir = embed_dir / "python_core"
|
||||
|
||||
if source_dir.exists():
|
||||
if target_dir.exists():
|
||||
shutil.rmtree(target_dir)
|
||||
|
||||
shutil.copytree(source_dir, target_dir)
|
||||
print("✅ python_core module copied")
|
||||
else:
|
||||
print("❌ python_core directory not found")
|
||||
|
||||
def create_launcher_script(embed_dir):
|
||||
"""创建启动脚本"""
|
||||
print("📝 Creating launcher script...")
|
||||
|
||||
launcher_content = '''@echo off
|
||||
REM Python Core Launcher Script
|
||||
set PYTHON_DIR=%~dp0
|
||||
set PYTHONPATH=%PYTHON_DIR%;%PYTHON_DIR%\\python_core
|
||||
"%PYTHON_DIR%\\python.exe" -m python_core.main %*
|
||||
'''
|
||||
|
||||
launcher_file = embed_dir / "python-core.bat"
|
||||
launcher_file.write_text(launcher_content)
|
||||
print("✅ Launcher script created")
|
||||
|
||||
def test_embedded_python(embed_dir):
|
||||
"""测试嵌入式Python"""
|
||||
print("🧪 Testing embedded Python...")
|
||||
|
||||
python_exe = embed_dir / "python.exe"
|
||||
|
||||
# 测试Python版本
|
||||
try:
|
||||
result = subprocess.run([
|
||||
str(python_exe), "--version"
|
||||
], capture_output=True, text=True)
|
||||
|
||||
if result.returncode == 0:
|
||||
print(f"✅ Python version: {result.stdout.strip()}")
|
||||
else:
|
||||
print(f"❌ Python test failed: {result.stderr}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error testing Python: {e}")
|
||||
return False
|
||||
|
||||
# 测试依赖
|
||||
test_imports = ["requests", "PIL", "json"]
|
||||
for module in test_imports:
|
||||
try:
|
||||
result = subprocess.run([
|
||||
str(python_exe), "-c", f"import {module}; print('{module} OK')"
|
||||
], capture_output=True, text=True)
|
||||
|
||||
if result.returncode == 0:
|
||||
print(f"✅ {module}: {result.stdout.strip()}")
|
||||
else:
|
||||
print(f"❌ {module} test failed: {result.stderr}")
|
||||
except Exception as e:
|
||||
print(f"❌ Error testing {module}: {e}")
|
||||
|
||||
# 测试python_core模块
|
||||
try:
|
||||
result = subprocess.run([
|
||||
str(python_exe), "-c", "import python_core; print('python_core OK')"
|
||||
], capture_output=True, text=True, cwd=embed_dir)
|
||||
|
||||
if result.returncode == 0:
|
||||
print(f"✅ python_core: {result.stdout.strip()}")
|
||||
else:
|
||||
print(f"❌ python_core test failed: {result.stderr}")
|
||||
except Exception as e:
|
||||
print(f"❌ Error testing python_core: {e}")
|
||||
|
||||
return True
|
||||
|
||||
def update_tauri_config(project_root, embed_dir):
|
||||
"""更新Tauri配置"""
|
||||
print("⚙️ Updating Tauri configuration...")
|
||||
|
||||
import json
|
||||
|
||||
config_file = project_root / "src-tauri" / "tauri.conf.json"
|
||||
|
||||
if config_file.exists():
|
||||
with open(config_file, 'r', encoding='utf-8') as f:
|
||||
config = json.load(f)
|
||||
|
||||
# 添加Python嵌入式目录到资源
|
||||
if 'bundle' not in config:
|
||||
config['bundle'] = {}
|
||||
|
||||
if 'resources' not in config['bundle']:
|
||||
config['bundle']['resources'] = []
|
||||
|
||||
python_resource = "python-embed/**"
|
||||
if python_resource not in config['bundle']['resources']:
|
||||
config['bundle']['resources'].append(python_resource)
|
||||
|
||||
# 写回配置
|
||||
with open(config_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(config, f, indent=2, ensure_ascii=False)
|
||||
|
||||
print("✅ Tauri configuration updated")
|
||||
else:
|
||||
print("❌ Tauri config file not found")
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("🚀 Setting up embedded Python for MixVideo V2...")
|
||||
print("=" * 50)
|
||||
|
||||
project_root = get_project_root()
|
||||
print(f"Project root: {project_root}")
|
||||
|
||||
try:
|
||||
# 1. 下载嵌入式Python
|
||||
embed_dir = download_embedded_python(project_root)
|
||||
|
||||
# 2. 设置pip
|
||||
if not setup_pip(embed_dir):
|
||||
print("❌ Failed to setup pip")
|
||||
return 1
|
||||
|
||||
# 3. 安装依赖
|
||||
install_dependencies(embed_dir)
|
||||
|
||||
# 4. 复制python_core模块
|
||||
copy_python_core(project_root, embed_dir)
|
||||
|
||||
# 5. 创建启动脚本
|
||||
create_launcher_script(embed_dir)
|
||||
|
||||
# 6. 测试
|
||||
if not test_embedded_python(embed_dir):
|
||||
print("❌ Embedded Python test failed")
|
||||
return 1
|
||||
|
||||
# 7. 更新Tauri配置
|
||||
update_tauri_config(project_root, embed_dir)
|
||||
|
||||
print("\n🎉 Embedded Python setup completed successfully!")
|
||||
print(f"📁 Python location: {embed_dir}")
|
||||
print("\n📋 Next steps:")
|
||||
print("1. Test the embedded Python:")
|
||||
print(f" {embed_dir}\\python.exe --version")
|
||||
print("2. Build your Tauri app:")
|
||||
print(" cargo tauri build")
|
||||
print("3. The Python environment will be bundled with your app")
|
||||
|
||||
return 0
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Setup failed: {e}")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
71
scripts/test_embedded_python.bat
Normal file
71
scripts/test_embedded_python.bat
Normal file
@@ -0,0 +1,71 @@
|
||||
@echo off
|
||||
REM 测试嵌入式Python环境
|
||||
|
||||
echo 🧪 Testing Embedded Python Environment...
|
||||
echo.
|
||||
|
||||
REM 切换到项目根目录
|
||||
cd /d "%~dp0.."
|
||||
|
||||
REM 检查嵌入式Python是否存在
|
||||
if not exist "src-tauri\python-embed\python.exe" (
|
||||
echo ❌ Embedded Python not found
|
||||
echo Please run: scripts\setup_embedded_python.bat
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo ✅ Embedded Python found
|
||||
|
||||
REM 测试Python版本
|
||||
echo 📋 Testing Python version...
|
||||
src-tauri\python-embed\python.exe --version
|
||||
if errorlevel 1 (
|
||||
echo ❌ Python version test failed
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
REM 测试基础依赖
|
||||
echo 📦 Testing dependencies...
|
||||
src-tauri\python-embed\python.exe -c "import requests; print('✅ requests OK')"
|
||||
src-tauri\python-embed\python.exe -c "import PIL; print('✅ Pillow OK')"
|
||||
src-tauri\python-embed\python.exe -c "import json; print('✅ json OK')"
|
||||
|
||||
REM 测试python_core模块
|
||||
echo 🔧 Testing python_core module...
|
||||
src-tauri\python-embed\python.exe -c "import python_core; print('✅ python_core OK')"
|
||||
if errorlevel 1 (
|
||||
echo ❌ python_core module test failed
|
||||
echo The module might not be copied correctly
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
REM 测试主入口
|
||||
echo 🚀 Testing main entry point...
|
||||
src-tauri\python-embed\python.exe -m python_core.main --module test --action hello
|
||||
if errorlevel 1 (
|
||||
echo ❌ Main entry point test failed
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
REM 测试服务模块
|
||||
echo 🔧 Testing service modules...
|
||||
src-tauri\python-embed\python.exe -m python_core.main --module template_manager --action get_templates
|
||||
if errorlevel 1 (
|
||||
echo ❌ Service module test failed
|
||||
echo This is expected if using mock services
|
||||
)
|
||||
|
||||
echo.
|
||||
echo 🎉 All tests passed!
|
||||
echo.
|
||||
echo 📋 Next steps:
|
||||
echo 1. Start your Tauri app: cargo tauri dev
|
||||
echo 2. Go to Python Test page
|
||||
echo 3. Run tests - should show "Using embedded Python for execution"
|
||||
echo.
|
||||
|
||||
pause
|
||||
Reference in New Issue
Block a user