【发布时间】:2017-04-10 13:59:20
【问题描述】:
到目前为止,我已经使用了 Py2exe,但不确定如何添加与我在脚本中使用的 firefox 和其他导入包相关的 selenium Web 驱动程序依赖项。
我也探索了 Pyinstaller,但添加依赖项失败。
我是第一次做,所以请建议如何正确地做。
谢谢
【问题讨论】:
标签: python selenium py2exe pyinstaller
到目前为止,我已经使用了 Py2exe,但不确定如何添加与我在脚本中使用的 firefox 和其他导入包相关的 selenium Web 驱动程序依赖项。
我也探索了 Pyinstaller,但添加依赖项失败。
我是第一次做,所以请建议如何正确地做。
谢谢
【问题讨论】:
标签: python selenium py2exe pyinstaller
您可以使用 py2exe 将您的 python 脚本打包为独立的可执行文件。
默认情况下 py2exe 会打包所有导入的包。如果你也想打包浏览器,你可能必须使用便携式浏览器。
您可以将便携式浏览器作为数据添加到您的 py2exe 包中,并在初始化 webdriver 时指定实际路径。
您可以在下面的类中使用 executable_path 参数指定 firefox 二进制可执行文件。
webdriver.Firefox(self, firefox_profile=None,firefox_binary=None, timeout=30, capabilities=None, proxy=None, executable_path=geckodriver, firefox_options=None, log_path=geckodriver.log)
** 我没有添加评论的选项,所以写作为答案。
【讨论】:
需要在 setup.py 文件中指定 selenium webdriver 的位置。
以下代码应该会有所帮助:
from distutils.core import setup
import py2exe
# Change the path in the following line for webdriver.xpi
data_files = [('selenium/webdriver/firefox', ['C:/Python27/Lib/site-packages/selenium/webdriver/firefox/webdriver.xpi'])]
setup(
name='Name of app',
version='1.0',
description='Description of app',
author='author name',
author_email='author email',
url='',
windows=[{'script': 'test.py'}], # the main py file
data_files=data_files,
options={
'py2exe':
{
'skip_archive': True,
'optimize': 2,
}
}
)
【讨论】:
您可能想尝试 CX_Freeze,它添加了您的代码作为单个 .exe 运行所需的所有必要包/依赖项
pip install cx_Freeze
【讨论】:
您可以使用pyinstaller 或cx_freeze 创建python 脚本/应用程序的可执行文件。
pyinstaller 的命令:
pyinstaller.exe --onefile --windowed <python file name>
【讨论】: