yeongbin-jo / python-chromedriver-autoinstaller

The chromedriver auto installer for distribution.
MIT License
237 stars 77 forks source link

urllib.error - https in a cx_freeze application #29

Closed DOH-WLD0303 closed 2 years ago

DOH-WLD0303 commented 2 years ago

Hi folks,

I'm running into a strange issue that seems to only occur on machines of users outside of my team. For context, I'm building an msi file for our end users using cx_freeze. I have a tkinter GUI application that on start up gets the chromedriver version that is packaged with the msi file and the chromedriver version that the user requires. The major version numbers are compared to determine if chromedriver_autoinstaller.install needs to be run.

The error occurs in the install function, I've included the traceback below.

Note: Line 2-7 are print statements from the application. Line 2 shows the directory that the packaged chromedriver lives (I've verified that it is there manually).

C:\Users\USERNAME\AppData\Local\Programs\DSSU - Case Lock Release Tool> app.exe
Chrome Driver Location: C:\Users\USERNAME\AppData\Local\Programs\DSSU - Case Lock Release Tool\supporting\chromedriver.exe
Comparing Version Numbers...
Existing Version: None
Latest Version: 101
Attempting to remove the old driver version...
No driver found, installing the latest version.
Traceback (most recent call last):
File "C:\Miniconda\envs\elrTool\Lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 113, in run
File "C:\Miniconda\envs\elrTool\Lib\site-packages\cx_Freeze\initscripts\Console.py", line 15, in run
File "app.py", line 183, in <module>
File "app.py", line 170, in main
File "D:\a\CaseLock-TimeSave\CaseLock-TimeSave\auto_update_fcns.py", line 45, in compare_major_versions
File "C:\Miniconda\envs\elrTool\lib\site-packages\chromedriver_autoinstaller\__init__.py", line 20, in install
File "C:\Miniconda\envs\elrTool\lib\site-packages\chromedriver_autoinstaller\utils.py", line 199, in download_chromedriver
File "C:\Miniconda\envs\elrTool\lib\site-packages\chromedriver_autoinstaller\utils.py", line 165, in get_matched_chromedriver_version
File "C:\Miniconda\envs\elrTool\lib\urllib\request.py", line 222, in urlopen
File "C:\Miniconda\envs\elrTool\lib\urllib\request.py", line 525, in open
File "C:\Miniconda\envs\elrTool\lib\urllib\request.py", line 547, in _open
File "C:\Miniconda\envs\elrTool\lib\urllib\request.py", line 502, in _call_chain
File "C:\Miniconda\envs\elrTool\lib\urllib\request.py", line 1425, in unknown_open
urllib.error.URLError: <urlopen error unknown url type: https>
PS C:\Users\USERNAME\AppData\Local\Programs\DSSU - Case Lock Release Tool>

The code responsible for running doing this is below:

############################################
# A set of functions for auto updating the chrome driver
############################################
import os
import shutil
import subprocess
import chromedriver_autoinstaller

def check_existing_driver(app_directory):
    chrome_driver_location = os.path.join(app_directory, "supporting", "chromedriver.exe")
    print(f'Chrome Driver Location: {chrome_driver_location}')

    if os.path.exists(chrome_driver_location):
        existing_version = subprocess.run(f'{chrome_driver_location} --version', capture_output=True, text=True).stdout.split()[1] # grabs the version number
        existing_version = existing_version.split('.')[0]
    else:
        existing_version = None

    return existing_version

# wrapped so we can check with test
def check_client_chrome_version():
    return chromedriver_autoinstaller.get_chrome_version().split('.')[0]

def install_cleanup(app_directory, latest_version):
    chrome_driver_location = os.path.join(app_directory, "supporting", latest_version, "chromedriver.exe")
    chrome_driver_final_location = os.path.join(app_directory, "supporting")
    shutil.copy(chrome_driver_location, chrome_driver_final_location,   )

def compare_major_versions(app_directory, existing_version, latest_version=check_client_chrome_version()):
    chrome_driver_location = os.path.join(app_directory, "supporting", "chromedriver.exe")
    print('Comparing Version Numbers...')
    print(f'Existing Version: {existing_version}')
    print(f'Latest Version: {latest_version}')
    if latest_version != existing_version: # compare major version number
        # remove the existing installer
        print('Attempting to remove the old driver version...')
        try:
            os.remove(chrome_driver_location)
        except:
            print("No driver found, installing the latest version.")
        finally:
            chromedriver_autoinstaller.install(path=os.path.join(app_directory, "supporting"))
            install_cleanup(app_directory=app_directory, latest_version=latest_version)
    else:
        print("The existing Chrome driver is sufficient.")

I'm very much at a loss of what the issue is. Is there perhaps an issue with the autoinstaller when frozen with cx_freeze?

kim-do-hyeon commented 2 years ago

Just use this code... import ssl

DOH-WLD0303 commented 2 years ago

My apologies, I had forgotten to loop back on this thread and close it. I was able to come to a solution with the help of the cx_freeze repo owner, (discussion found here). What was happening is that cx_freeze wasn't packaging up the dlls required for SSL with Python. I added the necessary steps to my setup.py script to package those into my application and was able to resolve the problem.

Just as a note though, import ssl will not fix this issue in a frozen python exe, at least not for cx_freeze. Other freeze packages may differ in this instance.