Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
669 views
in Technique[技术] by (71.8m points)

python - Is there a way to bundle a binary file (such as chromedriver) with a single file app/exe compiled with Pyinstaller?

As noted in the answer to my question here, setting the path to chromedriver in binaries in the Pyinstaller spec file (binaries=[('/usr/bin/chromedriver', './selenium/webdriver')]) didn’t have an effect (unless it was set incorrectly). That is, chromedriver is accessed as long as it’s in the PATH (/usr/bin in this case). My question regards the possibility to bundle chromedriver in the background so that it doesn’t have to be manually installed on another machine.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I succesfully bundled chromedriver with pyinstaller (although unfortunately, my virusscanner flagged it, after I ran the exe, but that's another problem)

I guess your problem is that you do not give the correct path to the webdriver in the script (using keyword executable_path). Also, I included the chromedriver as a data-file, although I'm not sure if that makes a difference..

Here is my example.

sel_ex.py:

from selenium import webdriver

import os, sys, inspect     # http://stackoverflow.com/questions/279237/import-a-module-from-a-relative-path
current_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe() ))[0]))

def init_driver():
    chromedriver = os.path.join(current_folder,"chromedriver.exe")
    # via this way, you explicitly let Chrome know where to find 
    # the webdriver.
    driver = webdriver.Chrome(executable_path = chromedriver) 
    return driver

if __name__ == "__main__":
    driver = init_driver()
    driver.get("http://www.imdb.com/")

sel_ex.spec:

....
binaries=[],
datas=[("chromedriver.exe",".")],
....

In this way, the chromedriver was stored in the main folder, although it should not matter where it is stored, as long as the script correct path through the keyword executable_path

disclaimers: -I did not use the one-file-settings, but that shouldn't make a difference. -my OS is windows


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...