6

I am trying to add Chromedriver inside an executable in pyinstaller. While this is possible it seems that I get the below error message when trying to run this on another computer.

I have tried a number of posts including this one, but unfortunately, this has not provided desired results. Best case was I could run it on my own computer when chrome exe was in the same folder which was unhelpful.

Code 1:

Main.py

from selenium import webdriver
driver = webdriver.Chrome()

What I get when running on another pc:

Error 1:

Cannot find Chrome Path

   C:\Users\Aperture Science\Desktop\1>123.exe
    Traceback (most recent call last):
      File "site-packages\selenium\webdriver\common\service.py", line 74, in start
      File "subprocess.py", line 709, in __init__
      File "subprocess.py", line 997, 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 "main.py", line 42, in <module>
      File "main.py", line 33, in main
      File "site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
      File "site-packages\selenium\webdriver\common\service.py", line 81, 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

    [2228] Failed to execute script main

How can I get around this?

What I get from link provided:

Code 2:

from selenium import webdriver
import os, sys, inspect
current_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe() ))[0]))
chromedriver = os.path.join(current_folder,"chromedriver.exe")
driver = webdriver.Chrome(executable_path = chromedriver)
driver.get("http://www.imdb.com/")

REQUIRES Chrome exe in set path, bundled chrome not read. So packaged chrome does not work as desired.

  • 1
    So what is your Question? Where are you stuck? Seeing any error? Update the question with error stack trace please. – undetected Selenium Dec 07 '17 at 09:45
  • The [link](https://stackoverflow.com/questions/41030257/is-there-a-way-to-bundle-a-binary-file-such-as-chromedriver-with-a-single-file) from your post has the answer, but some reason your code is ignoring it. Why would you expect your example to work given that the targeted machine obviously doesn't have the driver present on the `PATH` environment variable and given that you are not providing the path to `webdriver.Chrome()` ? – Florent B. Dec 12 '17 at 18:02
  • @FlorentB. I get a separate error I've posted. I also have to put chromedriver in a folder beside executable as it does not want to detect chrome packaged. I'm not entirely sure how you get around chrome binary for current folder though. –  Dec 12 '17 at 18:04
  • the same thing I am right now accomplishing , so you can do like this : read the chrome drive in the binary form using "wb" handler and save it in the variable , then pass this value to the program and let it first create the executable of chromedriver by writing the binary data to "chrome.exe" and then you can import it in your program. – babygame0ver Dec 12 '17 at 18:13
  • @babygame0ver Can you show an example of how this is achieved? –  Dec 12 '17 at 18:16
  • @FlorentB. Message: 'Scripts' executable may have wrong permissions –  Dec 12 '17 at 18:17
  • place the chromedriver file in the same folder of the exe , unfortunately you have to ship the chromedrive.exe with the executable , but only thing you can do is first download the chromedriver and then use it in the program – babygame0ver Dec 12 '17 at 18:28
  • @babygame0ver Are you saying it is impossible for chromedriver to be read inside of pyinstaller exe? No workaround? –  Dec 12 '17 at 18:31
  • frankly speaking bro, I am working on a simple project where my bitch teacher used to add question on an web interface for the students so , she has to do manually ,I used selenium and so i made the exe file of the actual python file and then also shipped the chromedriver.exe with the program. The problem in your code is that the script is not finding the chromedriver.exe path , so place the chromedriver.exe file in the the same directory of python file. It will work – babygame0ver Dec 12 '17 at 18:38
  • @babygame0ver It tends to work fine on my pc, its when I use another pc this tends to happen. I mean I suppose a workaround could be if it was installed on C drive always. Not super flexible but possible I guess. I'll see if that works. A shame no support for packaged chrome exe. Well it gets packaged, reading it is another question. –  Dec 12 '17 at 18:46
  • Ship both of the executables in the same directory but before that change your python this in your script to driver = webdriver.Chrome('chromedriver.exe') – babygame0ver Dec 12 '17 at 18:49

2 Answers2

14

Use --add-binary to bundle the driver in the application:

pyinstaller -F --add-binary "C:\drivers\chromedriver.exe";"." script.py

and use sys._MEIPASS to get the folder where the driver is extracted:

import sys, os, time
from selenium import webdriver

if __name__ == "__main__":

  if getattr(sys, 'frozen', False): 
    # executed as a bundled exe, the driver is in the extracted folder
    chromedriver_path = os.path.join(sys._MEIPASS, "chromedriver.exe")
    driver = webdriver.Chrome(chromedriver_path)
  else:
    # executed as a simple script, the driver should be in `PATH`
    driver = webdriver.Chrome()

  driver.get("https://stackoverflow.com")
  time.sleep(5)

  driver.quit()
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • This gives: Unable to find "C:\drivers\chromedriver.exe" when adding binary and data files.. Does this work for chrome packages inside of pyinstaller? –  Dec 12 '17 at 19:40
  • 1
    Change the path to the location of chromedriver.exe on your machine. – Florent B. Dec 12 '17 at 19:42
  • I used spec file instead. This is quite interesting. Will see if it this works again. –  Dec 12 '17 at 19:44
0

Or just simply:

pyinstaller --onefile script.py --paths "C:\drivers\chromedriver.exe"
gest
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – user11717481 Apr 22 '22 at 21:59