【发布时间】:2014-02-01 15:01:36
【问题描述】:
我尝试在 exe 文件中构建我的 python selenium 测试并在许多机器上运行它以保持测试独立于环境。但结果 *.exe 文件找不到 selenium webdriver。如何在 *.exe 文件中包含所有 selenium 依赖项?或者也许还有其他方法? 是否可以制作虚拟环境并分发?
【问题讨论】:
标签: python selenium pyinstaller
我尝试在 exe 文件中构建我的 python selenium 测试并在许多机器上运行它以保持测试独立于环境。但结果 *.exe 文件找不到 selenium webdriver。如何在 *.exe 文件中包含所有 selenium 依赖项?或者也许还有其他方法? 是否可以制作虚拟环境并分发?
【问题讨论】:
标签: python selenium pyinstaller
我假设您使用 py2exe 来生成 exe。您需要在 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', ['D:/Python27/Lib/site-packages/selenium/webdriver/firefox/webdriver.xpi'])]
setup(
name='General name of app',
version='1.0',
description='General description of app',
author='author name',
author_email='author email',
url='',
windows=[{'script': 'abc.py'}], # the main py file
data_files=data_files,
options={
'py2exe':
{
'skip_archive': True,
'optimize': 2,
}
}
)
【讨论】:
与大多数其他二进制文件一样,可能需要在二进制文件中包含 DLL 或您需要的任何库。例如:
C:\tests\
run_tests.exe -- this will read from webdriver.dll
selenium-webdriver.dll
此外,从我的 .NET 时代开始,我就知道您实际上能够将库直接嵌入到 EXE 中,这使得它相当大。
【讨论】:
【讨论】:
这是旧的,但我一直在寻找同样的东西,我确实不得不在许多不同的网站上挖掘我的问题,所以希望这对其他人有帮助。
我正在使用 py2exe 构建我的 exe 文件,但它不起作用,所以我决定尝试 pyinstaller,是的,它起作用了。
只是将物品放入物品中以更有条理:
Py2exe: 我首先从 py2exe 开始,我收到这样的错误: python setup.py py2exe Invalid Syntax (asyncsupport.py, line 22)
我可以通过删除安装文件中的一些东西来修复它,最后看起来是这样的。
data_files = [('selenium/webdriver/chrome', ['C:\Python27\Lib\site-packages\selenium\webdriver\chrome\webdriver.py'])]
setup(
name='General name of app',
version='1.0',
description='General description of app',
author='author name',
author_email='author email',
url='',
windows=[{'script': 'final_headless.py'}], # the main py file
data_files=data_files,
options={
'py2exe':
{
'skip_archive': True,
'optimize': 2,
'excludes': 'jinja2.asyncsupport',
'dll_excludes': ["MSVCP90.dll","HID.DLL", "w9xpopen.exe"]
}
}
)
它可以运行py2exe,但exe文件不起作用,然后我搬到了pyinstaller。
安装程序: 对我来说,pyinstaller 看起来比 py2exe 容易得多,所以我从现在开始就坚持这一点。我们唯一的“问题”是路径中没有 webdriver,exe 无法运行。 但是,只要将它放在变量路径中,就可以了。
结论,使用 pyinstaller 是我的解决方案 + 将 webdriver 添加到路径。
【讨论】: