import tarfile import os import subprocess import shutil BASE_DIR = os.path.dirname(os.path.abspath(__file__)) PYTHON_PATH = os.path.join(BASE_DIR, 'runtime/python.exe') pip_folder = 'pip-23.3' extract_dir = os.path.join(BASE_DIR, 'runtime/pip') # Open the tar.gz file with tarfile.open(f'./runtime/{pip_folder}.tar.gz', 'r:gz') as tar: tar.extractall(path=extract_dir) print("Extraction completed.") print("Extracted to directory:", extract_dir) pip_install_path = os.path.join(BASE_DIR, 'runtime/pip', pip_folder) setup_py_path = os.path.join(pip_install_path, 'setup.py') # 构建安装命令 install_command = [PYTHON_PATH, setup_py_path, 'install'] # 打印执行的命令 print("Executing command:", ' '.join(install_command)) subprocess.check_call(install_command, cwd=pip_install_path) print("pip has been installed successfully.") # 开始安装包 install_command_2 = [PYTHON_PATH, '-m', 'pip', 'install', '-r', 'requirements.txt', '-i', 'https://pypi.tuna.tsinghua.edu.cn/simple'] # 打印执行的命令 print("Executing command:", ' '.join(install_command)) subprocess.check_call(install_command_2) print("package has been installed successfully.") # 现在删除解压的文件夹 try: shutil.rmtree(extract_dir) print(f"Successfully removed the extracted folder: {extract_dir}") except Exception as e: print(f"Error removing the extracted folder: {e}")