有没有办法将二进制文件(例如 chromedriver)与使用 Pyinstaller 编译的单个文件 app/exe 捆绑在一起? [英] Is there a way to bundle a binary file (such as chromedriver) with a single file app/exe compiled with Pyinstaller?

查看:70
本文介绍了有没有办法将二进制文件(例如 chromedriver)与使用 Pyinstaller 编译的单个文件 app/exe 捆绑在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如对我的问题的回答所述此处,在 Pyinstaller 规范文件中的 binaries 中设置 chromedriver 的路径(binaries=[('/usr/bin/chromedriver', './selenium/webdriver')]) 没有效果(除非设置不正确).也就是说,只要 chromedriver 位于 PATH 中(在本例中为/usr/bin),它就会被访问.我的问题是关于在后台捆绑 chromedriver 的可能性,这样它就不必手动安装在另一台机器上.

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.

推荐答案

我成功地将 chromedriver 与 pyinstaller 捆绑在一起(虽然不幸的是,我的virusscanner 在我运行 exe 后标记了它,但这是另一个问题)

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

我猜您的问题是您没有在脚本中提供正确的 webdriver 路径(使用关键字 executable_path).另外,我将 chromedriver 作为数据文件包含在内,尽管我不确定这是否有所不同..

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..

这是我的例子.

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:

sel_ex.spec:

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

这样,chromedriver 就存储在主文件夹中了,虽然存储在哪里应该没有关系,只要脚本通过关键字 executable_path 正确路径

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

免责声明:- 我没有使用单一文件设置,但这应该没什么区别.-我的操作系统是windows

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

这篇关于有没有办法将二进制文件(例如 chromedriver)与使用 Pyinstaller 编译的单个文件 app/exe 捆绑在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆