【问题标题】:Is there a way to get a list of processus with PyInstaller windowed?有没有办法通过 PyInstaller 窗口化获取进程列表?
【发布时间】:2019-12-03 14:30:34
【问题描述】:

我正在尝试测试浏览器是否在一个转换为exepyinstaller 的python 程序上打开,有没有办法在不显示窗口并将其转换为exe 的情况下进行测试?

我尝试了 subprocess,但它在 pyinstaller 的窗口模式下产生错误,我无法将 psutil 导入 pyinstaller ("can't import psutil" error)

我使用subprocess的代码:

enter code heresubprocess.check_output('tasklist', shell=True)

程序未启动并出现错误消息:“执行脚本失败” 此处发布了类似的错误:subprocess seems not working in pyinstaller exe file

提前感谢您的回答

【问题讨论】:

  • 你能添加更多代码吗?如果没有代码 sn-p 和特定于平台的详细信息,很难掌握您正在处理的问题
  • 当然:配置:Windows-10-10.0.0.17134-SP0 Python 3.7.4 PyInstaller:3.4 对于代码:这是一个只有在浏览器打开时才会减少的计时器:全局计时器; e=str(subprocess.check_output('tasklist', shell=True));如果e中的“chrome.exe”或e中的“firefox.exe”或e中的“iexplore.exe”:;计时器-=1;

标签: python process pyinstaller windowed


【解决方案1】:

如果您只是想检查一个进程是否正在运行,您可以使用psutil.process_iter,但由于 Pyinstaller 无法完全解析 psutil 模块,您需要使用 add-data 标志将整个 Lib 文件夹添加到您的输出可执行文件:

import psutil
process_to_find = "chrome.exe"
process_list = [p.name() for p in list(psutil.process_iter())]
if process_to_find in process_list:
   # do whatever you want
   print("Process found!")

接下来使用以下命令生成可执行文件(您也可以使用-w 标志):

pyinstaller -F --add-data "<python_path>\Lib\site-packages\psutil;./psutil" script.py 

【讨论】:

  • 成功了,谢谢!附言: 我的psutil还有一个问题,它是在第二个python安装中,如果其他人无法导入它,请检查它是否在您正在使用的python的LIB文件夹中。否则,只需从其他 python 复制它。
猜你喜欢
  • 2013-05-09
  • 2016-12-04
  • 1970-01-01
  • 1970-01-01
  • 2011-02-14
  • 2021-12-10
  • 2020-08-15
  • 2010-09-09
  • 1970-01-01
相关资源
最近更新 更多