首页 > 解决方案 > 扩展名为 .ipynb/.py 的 Python 程序在转换为 .exe 时不起作用?

问题描述

我正在尝试创建一个程序的可执行文件,该程序会打开一个链接并每半小时进行一次登录并继续计算时间。它是用 Jupyter Notebook 编写的。该代码在 Jupyter(.ipynb)/.py 中运行良好,但是当转换为 .exe 时,它​​会抛出一个错误,说"Fatal error Detected -Failed to execute script". 代码如下:

from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from plyer import notification

i=1
while i<=20:
    notification.notify(
    title="Initiating Session Number {}".format(i),
    message='Marking Attendance',
    app_icon=r"C:\Users\91800\Downloads\Documents\automation\aut.ico", 
    timeout=6,  # seconds
    )
    options = webdriver.ChromeOptions()
    options.add_argument('--ignore-ssl-errors=yes')
    options.add_argument('--ignore-certificate-errors')
    options.add_argument('--allow-running-insecure-content')
    driver = webdriver.Chrome(options=options)

    driver.get('https://120.72.92.102:10443/remote/login?lang=en')
    WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "username")))

    username = driver.find_element_by_id("username")
    password = driver.find_element_by_id("credential")

    username.send_keys("pranjal.pathak")
    password.send_keys("zxc^567")

    driver.find_element_by_id("login_button").click()
    time.sleep(10)
    driver.close()

我收到以下错误(对不起,图像质量,这是我能做到的最好的):

在此处输入图像描述

标签: pythonpython-3.xjupyter-notebook

解决方案


好的。因此,由于没有人回答我的问题,我认为如果我自己澄清一下就好了,因为我已经弄清楚了。

我设法通过进行 2 项更改使其工作 - 1. 使用 win10toast 进行通知而不是 plyer 2. 确保在从命令提示符创建 .exe 时导入图像。为此,我在命令提示符下使用了这个命令:

pyinstaller -F --onefile -i "C:\user\.....(location of the image)" filename.py

这优雅地解决了我的问题。


推荐阅读