首页 > 解决方案 > 我找不到答案如何从包含所有依赖库的 py 文件中制作 exe

问题描述

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class UntitledTestCase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.google.com/"
        self.verificationErrors = []
        self.accept_next_alert = True
    
    def test_untitled_test_case(self):
        driver = self.driver
        driver.get("https://example.com")
        driver.find_element_by_xpath("(//button[@type='button'])[13]").click()
        driver.find_element_by_xpath("(//button[@type='button'])[26]").click()
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e: return False
        return True
    
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True
    
    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

我希望将此 python 文件转换为 exe。我尝试过使用 pyinstaller,但它不会编译像 selenium 这样的依赖项,即使使用 --onefile 参数也是如此。有没有办法用这个 python 脚本制作一个 .exe 文件?我已经进行了如下搜索,但我找不到任何明确的答案。

如何将python脚本编译为二进制可执行文件

https://datatofish.com/executable-pyinstaller/

将 Python 文件转换为 .exe,包括库

PS 我不想在“编译列表”中手动包含包,因为我有数百个 py 文件要转换。

谢谢。

标签: pythonpyinstallerexecutable

解决方案


看看这个以前用多种解决方案回答过的问题,而不仅仅是使用 pyinstaller:

从 Python 项目创建单个可执行文件


推荐阅读