[Python] Error when using PyInstaller to create an executable: 'session not created: failed to...

Discussão em 'Python' iniciado por Stack, Abril 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have an app that needs to open Chrome and automatically send WhatsApp messages. When I run python main.py, everything works as expected. However, when I try to generate a .exe using PyInstaller so that non-Python users can run it, I get the following error: session not created: failed to start Chrome process.

    Here is the relevant information:

    The chrome.exe and chromedriver versions I am using: both 135.0.7049.84

    The code that performs this task:

    import pickle
    import time
    import urllib.parse
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from webdriver_manager.chrome import ChromeDriverManager

    def run():
    with open("messages.pkl", "rb") as file:
    messages = pickle.load(file)

    options = webdriver.ChromeOptions()
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument(r"--user-data-dir=C:\Users\Urano\AppData\Local\Google\Chrome\User Data")
    options.add_argument("--profile-directory=Default")
    service = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=options)

    wait = WebDriverWait(driver, 30)

    print("Abriendo WhatsApp Web...")
    driver.get("https://web.whatsapp.com")
    input("Escaneá el código QR y presioná ENTER cuando WhatsApp Web esté cargado...\n")

    for phone, message in messages:
    try:
    print(f"\n Preparando mensaje para {phone}")
    encoded_message = urllib.parse.quote(message)
    url = f"https://web.whatsapp.com/send?phone={phone}&text={encoded_message}"
    driver.get(url)
    print(" Esperando que cargue el campo de texto...")
    message_box = wait.until(
    EC.presence_of_element_located((By.XPATH, '//div[@data-tab="10"]'))
    )
    print("✅ Campo de texto localizado")

    time.sleep(2)

    message_box.send_keys(Keys.ENTER)
    print(f"✅ Mensaje enviado a {phone}")

    time.sleep(3)

    except Exception as e:
    print(f"❌ Error al enviar mensaje a {phone}: {e}")
    continue

    driver.quit()
    print("\n✅ Todos los mensajes procesados.")

    if __name__ == "__main__":
    run()


    And my project structure (before running any pyinstaller command): project structure

    Any help on how to run the pyinstaller to fix this issue would be greatly appreciated!

    Continue reading...

Compartilhe esta Página