How to include chromedriver with pyinstaller?
Asked Answered
W

2

14

I am using pyinstaller to create an executable of my python script.
In the script I'm using these imports:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
etc...

The problem is, when running pyinstaller myscript.py , it will result in including Firefox, instead of Chrome. In the result folder c:...\dist\myscript\selenium\webdriver there is a firefox folder, so it is simply skipping chromedriver, and it is a serious problem for me, because the script needs to run with Chrome.
There is only a few questions around this topic, but there is no answer to solve the issue.
I was thinking on adding the --hidden-import MODULENAME tag to the command, but chromedriver.exe is not a module... Thanks

Warford answered 19/9, 2016 at 0:23 Comment(5)
You could add is as a data file, but I am not sure that it will solve the issue.Astrobiology
Thanks, I'll try this out. In the meantime, I found a workaround, that is, installing chromedriver on the target machine, and adding its folder to the Path, but I must find something without it.Warford
Why not adding your idea as an answer? :) It solved my problem, I added chromedriver.exe as a binary file to the spec. Thanks again!Warford
@Astrobiology sorry I forgot to mention you...Warford
I am glad it worked. You found the solution. I just gave you a hint.Astrobiology
W
12

It should be added as a binary file, since it is a binary file...
So a custom spec file needed where the chromedriver's path on the local system and the desired location relative to the dist\myscript should be defined, so it looks something like this:

.....
a = Analysis(['myscript.py'],
             pathex=['path\\to\\my\\script'],
             binaries=[ ('path\\to\\my\\chromedriver.exe', '.\\selenium\\webdriver') ],
             datas=None,
....

And then run the pyinstaller with this spec file: pyinstaller myscript.spec myscript.py

Warford answered 22/9, 2016 at 20:26 Comment(4)
I have a question regarding your answerSixtieth
Here is an updated questionSixtieth
Here is pyinstaller example explained in details. hackernoon.com/…. And here how packaging works python-packaging.readthedocs.io/en/latest/everything.htmlLegofmutton
How about firefox webdriver?Corps
E
0

I had this issue and solved it by adding the following code into my Spec file.

These paths are for windows OS:

a = Analysis(
    ['myproject.py'],
    pathex=[],
    binaries=[('C:\\chromedriver\\chromedriver.exe','Drivers')],
    datas=None,
)

and then pressed Shift + right click on my project folder to open PowerShell window.

Then use this:

pyinstaller  myproject.spec   myproject.py
Eld answered 25/2, 2024 at 14:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.