Hugo Iriarte
Hugo Iriarte

Reputation: 65

Python Pyinstaller GUI Tkinter Selenium

I did not know how to create an executable python program before I asked here. Thankfully I received a fast answer and was able to convert my script to an executable program. The executable works perfect but only on my computer. These are the two error's I am receiving, I feel like I need to modify the script in order to locate the chrome driver I am not sure where Pyinstaller saved everything.

Exception in Tkinter callback
Traceback (most recent call last):
File "site-packages\selenium\webdriver\common\service.py", line 76, in start
File "subprocess.py", line 775, in __init__
File "subprocess.py", line 1178, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "tkinter\__init__.py", line 1705, in __call__
File "MarijuanaDoctors.py", line 25, in search
File "site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
File "site-packages\selenium\webdriver\common\service.py", line 83, in start
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' 
executable needs to be in PATH. Please see 
https://sites.google.com/a/chromium.org/chromedriver/home

Upvotes: 1

Views: 1210

Answers (1)

Kamal
Kamal

Reputation: 2554

You can bundle your "chromedriver.exe" along with your script using Pyinstaller like this:

pyinstaller --add-binary="localpathtochromedriver;." myscript.py

This will copy the "chromedriver.exe" file in the same folder as your main .exe(Or in case of single file option of pyinstaller, this fill will be extracted in temp folder while using exe program).

In your script you can check if you are running the script normally or from bundled(exe file) mode, and choose path to chromedriver.exe accordingly.(This change in script can be common for Single file/folder bundle option of pyinstaller)

import sys
if getattr(sys, 'frozen', False ):
    #Running from exe, so the path to exe is saved in sys._MEIPASS
    chrome_driver = os.path.join(sys._MEIPASS, "chromedriver.exe")
else:
    chrome_driver = 'localpathtochromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver)

You can read about this in docs here.

Limitation: The user of your .exe should have Chrome installed on their system and Chrome version should work with the chromedriver which is bundled.

Upvotes: 1

Related Questions