You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在未安装Python及PIP的电脑上运行Python关机脚本?

如何在未安装Python及PIP的电脑上运行音乐播放+关机脚本?

问题描述

我是编程新手,写了一个能播放音乐并执行关机操作的Python脚本,但朋友不愿意用PIP安装依赖,想知道怎么在没装Python和PIP的电脑上运行这个脚本。

脚本代码如下:

import os 
import time
import subprocess
import pyautogui
import sys
import win32gui, win32con
from PIL import ImageGrab

pyautogui.hotkey('winleft', 'd')

time.sleep(1)

script_dir = os.path.dirname(__file__)
rel_path = "ex/Volume.mp3"
abs_file_path = os.path.join(script_dir, rel_path)
os.startfile(abs_file_path)

time.sleep(2)

pyautogui.hotkey('winleft', 'd')

script_dir = os.path.dirname(__file__)
rel_path = "ex/Sys.vbs"
abs_file_path = os.path.join(script_dir, rel_path)
os.startfile(abs_file_path)

time.sleep(5)

os.system("taskkill /f /im wscript.exe")

pyautogui.hotkey('winleft', 'd')

script_dir = os.path.dirname(__file__)
rel_path = "ex/Image.jpg"
abs_file_path = os.path.join(script_dir, rel_path)
os.startfile(abs_file_path)

time.sleep(0.1)

hwnd = win32gui.GetForegroundWindow()
win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)

time.sleep(0.1)

script_dir = os.path.dirname(__file__)
rel_path = "ex.vbs"
abs_file_path = os.path.join(script_dir, rel_path)
os.startfile(abs_file_path)

time.sleep(0.2)

hwnd = win32gui.GetForegroundWindow()
win32gui.MoveWindow(hwnd, 830, 800, 200, 100, True)
bbox = win32gui.GetWindowRect(hwnd)
img = ImageGrab.grab(bbox)

time.sleep(4.8)

COMMAND  = """(New-Object -comObject Shell.Application).Windows() | foreach-object {$_.quit()}; Get-Process | Where-Object {$_.MainWindowTitle -ne \"\"} | stop-process; Stop-Computer"""
subprocess.run(['powershell', '-command', COMMAND], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

解决方案:打包成独立可执行文件

把Python脚本和所有依赖(包括解释器、第三方库、外部资源文件)打包成Windows可执行文件(.exe),朋友双击就能运行,无需安装Python或PIP。推荐两种常用工具:

方法1:PyInstaller(最常用)

步骤:

  1. 在你自己的电脑上安装PyInstaller:
pip install pyinstaller
  1. 执行打包命令,注意要把脚本依赖的外部资源文件一起打包:

    • 单文件打包(生成一个.exe,方便传输但启动稍慢):
    pyinstaller --onefile --add-data "ex/Volume.mp3;ex" --add-data "ex/Sys.vbs;ex" --add-data "ex/Image.jpg;ex" --add-data "ex.vbs;." your_script_name.py
    
    • 文件夹打包(生成多个文件,启动更快):
    pyinstaller --add-data "ex/Volume.mp3;ex" --add-data "ex/Sys.vbs;ex" --add-data "ex/Image.jpg;ex" --add-data "ex.vbs;." your_script_name.py
    

    注:Windows系统用;分隔路径,若用其他系统需换成:

  2. 打包完成后,dist文件夹里会生成可执行文件(或文件夹),把整个dist目录里的内容发给朋友即可。

方法2:cx_Freeze

步骤:

  1. 安装cx_Freeze:
pip install cx_Freeze
  1. 创建setup.py配置文件,内容如下:
import sys
from cx_Freeze import setup, Executable

# 替换成你的脚本文件名
script_name = "your_script_name.py"

# 指定要打包的外部资源文件
include_files = [
    ("ex/Volume.mp3", "ex/Volume.mp3"),
    ("ex/Sys.vbs", "ex/Sys.vbs"),
    ("ex/Image.jpg", "ex/Image.jpg"),
    ("ex.vbs", "ex.vbs")
]

# Windows下隐藏控制台窗口(如果不需要控制台输出)
base = "Win32GUI" if sys.platform == "win32" else None

executables = [Executable(script_name, base=base)]

setup(
    name="MusicShutdownTool",
    version="1.0",
    description="Play music and shut down computer",
    options={"build_exe": {"include_files": include_files}},
    executables=executables
)
  1. 运行打包命令:
python setup.py build
  1. 打包完成后,build文件夹下的对应系统目录(如exe.win-amd64-3.11)就是可运行程序,发给朋友即可。

重要注意事项

  • 系统兼容性:必须在和朋友电脑相同的Windows版本环境下打包,避免出现运行报错。
  • 资源文件:一定要把脚本用到的所有外部文件(ex文件夹里的内容、ex.vbs)都包含在打包命令里,不然运行时会提示找不到文件。

内容的提问来源于stack exchange,提问作者Core

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 19:15:50