Nuitka: How to solve the find_dotenv AssertionError that occurs when opening an exe after importing selenium.

Hello, I was successful in using nuitka to package it into an exe, but there was an error when starting the exe. Here is my environment Python installed using python-3.11.0-amd64.exe Nuitka installed using this command pip install nuitka pip install -U “https://github.com/Nuitka/Nuitka/archive/develop.zip” Operating System: Win10

Below is the simplified code from my project.

import requests
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import selenium.common.exceptions
import json

def login():
    print("Please choose a login method:")
    print("1. Log in through the browser and automatically extract cookies")
    choice = input()
    if choice == "1":
        print("Please select a browser:")
        print("1. Google Chrome")
        print("2. Microsoft Edge")
        choice = input()
        if choice == "1":
            option = webdriver.ChromeOptions()
            option.add_experimental_option("detach", True)
            option.add_experimental_option('excludeSwitches', ['enable-logging'])
            driver = webdriver.Chrome(ChromeDriverManager().install(), options=option)
        elif choice == "2":
            option = webdriver.EdgeOptions()
            option.add_experimental_option("detach", True)
            option.add_experimental_option('excludeSwitches', ['enable-logging'])
            driver = webdriver.Edge(EdgeChromiumDriverManager().install(), options=option)
        else:
            return
        print("Please log in to Ximalaya in the pop-up browser; the browser will automatically close upon successful login.")
        driver.get("https://passport.ximalaya.com/page/web/login")
        try:
            WebDriverWait(driver, 300).until(EC.url_to_be("https://www.ximalaya.com/"))
            cookies = driver.get_cookies()
            driver.quit()
        except selenium.common.exceptions.TimeoutException:
            print("Login timed out, automatically returning to the main menu!")
            driver.quit()
            return
        cookie = ""
        for cookie_ in cookies:
            cookie += f"{cookie_['name']}={cookie_['value']}; "
        with open("config.json", "r", encoding="utf-8") as f:
            config = json.load(f)
        config["cookie"] = cookie
        with open("config.json", "w", encoding="utf-8") as f:
            json.dump(config, f)
    else:
        print("Invalid choice, please restart the process.")
        return

if __name__ == '__main__':
    login()

The CLI command I used is:

nuitka --standalone --show-memory --show-progress --follow-import-to=need --output-dir=o --nofollow-imports --include-data-file=selenium-manager.exe=selenium-manager.exe test.py

When launching the exe, the following error occurs

Traceback (most recent call last):
  File "D:\CODE_T~1\o\TEST~1.DIS\test.py", line 2, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "D:\CODE_T~1\o\TEST~1.DIS\webdriver_manager\chrome.py", line 4, in <module webdriver_manager.chrome>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "D:\CODE_T~1\o\TEST~1.DIS\webdriver_manager\core\download_manager.py", line 5, in <module webdriver_manager.core.download_manager>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "D:\CODE_T~1\o\TEST~1.DIS\webdriver_manager\core\http.py", line 4, in <module webdriver_manager.core.http>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "D:\CODE_T~1\o\TEST~1.DIS\webdriver_manager\core\config.py", line 9, in <module webdriver_manager.core.config>
  File "D:\CODE_T~1\o\TEST~1.DIS\dotenv\main.py", line 346, in load_dotenv
  File "D:\CODE_T~1\o\TEST~1.DIS\dotenv\main.py", line 305, in find_dotenv
AssertionError

I noticed that inside the Selenium folder, there is a selenium-manager.exe Perhaps it’s because it automatically generates a .env file, but I don’t know how to resolve the above errors. thank

About this issue

  • Original URL
  • State: closed
  • Created 4 months ago
  • Comments: 21 (14 by maintainers)

Most upvoted comments

@artem3k with my fix, workarounds should not be necessary anymore, but all forms are supposed to work. Of course, specifying the path indeed avoids the problematic code, but I am not sure your variant does it, anyway, changing the code is never the preferred solution in Nuitka, we want to be 100% compatible with these kind of packages.