Sco*_*Duh 2 python executable cmd pyautogui
我正在使用一种工具将我的 python 文件转换为 .exe 文件。您可以在此视频中找到该工具py to .exe。
现在,当我转换此代码时,一切正常:
width = float(input("Enter the width: "))
height = float(input("Enter the height: "))
area = width * height
print("The area is", area, "square units.")
Run Code Online (Sandbox Code Playgroud)
但是每当我在其中添加一行带有 PyAutoGUI 模块的代码时,.exe 文件就会立即关闭并且什么也不做。
例子:
width = float(input("Enter the width: "))
pyautogui.moveTo(492, 106, 0.2)
height = float(input("Enter the height: "))
area = width * height
print("The area is", area, "square units.")
Run Code Online (Sandbox Code Playgroud)
如何防止 .exe 文件关闭以及为什么不执行 PyAutoGUI 代码?
此安装程序使用PYINSTALLER来创建 EXE。PYINSTALLER 在创建 .exe 时使用默认的 python 虚拟环境。因此,如果您使用的虚拟环境不是默认环境(conda env 或其他 venv),则 .exe 将无法运行。因此,当默认 env 准备好运行程序时,这意味着如果您在默认 env 中安装所有必需的包并使用 pyinstaller 则不会有问题。希望我正确地解决了这个问题。