Python 3.6 尝试使用来自另一个程序的命令

Python 3.6 trying to use commands from another program

我的部分 python 程序使用子进程打开 vbs 脚本。

path = os.sep.join(['C:','Users',getpass.getuser(),'Desktop','Program','build','exe.win32-3.6','vbs.vbs'])

subprocess.call([sys.executable, path])

但是它没有执行我的 vbs 脚本,而是尝试 运行 它作为 python 代码并给我:NameError: msgbox is not defined。 当我手动 运行 vbs 脚本时它工作。

我要python正常执行vbs脚本。不是 运行 而是 python 代码。

正是您告诉子进程要做的事情。来自 docs

sys.executable

A string giving the absolute path of the executable binary for the Python interpreter, on systems where this makes sense. If Python is unable to retrieve the real path to its executable, sys.executable will be an empty string or None.

sys.executable 指向系统的 Python 可执行文件。在您的情况下,可能类似于 C:\Python27\python.exe。你应该打印出来自己看看。

要执行 VBScript,您需要使用 C:\Windows\system32\wscript.exe

此外,使用 os.path.join()os.sep.join() 更适合该任务。

所以你最终会得到:

system32 = os.path.join(os.environ['SystemRoot'], 'system32')
wscript = os.path.join(system32, 'wscript.exe')
path = os.sep.join(['C:','Users',getpass.getuser(),'Desktop','Program','build','exe.win32-3.6','vbs.vbs'])
subprocess.call([wscript, path])