python - 代码在pycharm中运行良好,但转换为exe时却不行

标签 python selenium pyinstaller webdriver-manager

基本上我的代码在 Pycharm 中运行良好,但是当我使用 Pyinstaller 将其转换为 exe 时,它​​不起作用并给出错误。

我尝试了一些更改,但所有尝试都失败了。

Selenium .py

from selenium import webdriver
import ctypes
from webdriver_manager.firefox import GeckoDriverManager

#driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

user32 = ctypes.windll.user32
width = user32.GetSystemMetrics(0) # 2560 mine for example
height = user32.GetSystemMetrics(1) # 1440 mine for example

wT = width / 3
hT = height / 2
wB = width / 2
answer = height / 36
hB = (height / 2) - answer

wPa = width / 3
wPa2 = wPa * 2

hPa = height / 2
wPa3 = width / 2


def Window1():
    driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
    driver.get("https://www.google.co.uk/")
    driver.set_window_size(wT, hT)
    driver.set_window_position(0, 0)
    driver.switch_to.window(driver.window_handles[0])
if __name__ == '__main__':
    Window1()

运行.py

import threading
import Selenium
import time

t1 = threading.Thread(target=Selenium.Window1)

t1.start()
time.sleep(2)

^这是我的代码的精简版本,我的打开了 5 个窗口而不是 1 个,但这只是我正在做的事情/如何对其进行线程化的示例。希望这已经足够了。

这是我尝试运行 exe 时遇到的错误:

enter image description here

最佳答案

PyInstaller 似乎无法解析 webdriver_manager。因此,您只需将整个库添加为数据文件 add-data :

pyinstaller -F --add-data ".\env\Lib\site-packages\webdriver_manager;webdriver_manager" script.py

请记住将 webdriver_manager 路径替换为正确的路径。

关于python - 代码在pycharm中运行良好,但转换为exe时却不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56927510/

相关文章:

python - 使用标签显示图像

python - 无法将 xgboost 添加到 pyinstaller

python - 在 python 中使用列表理解扩展列表

python - 前向传播缓慢 - 训练时间正常

java - 使用 selenium webdriver 从文件中获取值并从下拉列表中选择相同的值

java - 如何在网页的某个框架(以div分隔)上向下滚动?

javascript - Python:Selenium 模拟 onclick

windows-7 - Pyinstaller,如何在 32 位 linux 上制作 32 位和 64 位 .exe?

python - 使用 pyinstaller 转换为 .exe 后退出 pygame 窗口且未打开控制台时出现 "Failed to execute script myscript"

python - 根据数字字符分割字符串的正则表达式模式