是否可以将.py和.ps1脚本合并为.exe?

时间:2018-11-27 12:33:32

标签: python powershell

我有一个python文件,其中包含gui'window.py',powershell脚本'get-aduser.ps1'。 是否可以将这两个捆绑为一个exe?

window.py文件包含一个组合框,该组合框在运行时运行.ps1脚本。然后,组合框将填充Powershell脚本的输出。

为什么要这么做:

我的同事应该在其客户端上运行程序,而不必安装python和所有使用的库。

每个人都有RSAT工具,因此在执行powershell脚本时这应该不是问题。

2 个答案:

答案 0 :(得分:1)

向消费者提供基于python的解决方案而不强迫他们进入python模块的一种通用方法是:pyinstaller

答案 1 :(得分:0)

我如何解决问题:

我在运行.exe时临时创建了powershell脚本,而不是尝试使用python文件打包到一个.exe中,如下所示:

with open("C:\\Temp\\newusers.ps1", 'w', encoding='utf8') as f:
f.write('$s = "OU=,DC=,DC="\n$User = Get-ADUser -Filter * -Properties name, employeeID -Searchbase $s | Select-Object -Property name,employeeID\n$User | Out-File -Encoding utf8 C:\\Users\\USER\\Desktop\\users.txt')`

并且在GUI关闭之后,我要删除.ps1脚本:

os.remove('C:\\Temp\\newusers.ps1')

将python文件打包到.exe中。

第一步:安装pyinstaller

pip install pyinstaller

第二步:使用它

pyinstaller -F C:\Path\to\your\file.py