如何为selenium web驱动程序中创建的.py文件创建.exe文件?

时间:2016-11-26 10:25:54

标签: python selenium py2exe pyinstaller

到目前为止,我已经使用了Py2exe但不确定如何添加与我在脚本中使用的firefox和其他导入包相关的selenium web驱动程序依赖项。

我还探索了Pyinstaller,但它在添加依赖项方面失败了。

我是第一次这样做,所以请建议如何正确地做到这一点。

谢谢

4 个答案:

答案 0 :(得分:1)

您可以使用py2exe将python脚本打包为独立的可执行文件。

默认情况下,py2exe打包所有导入的包。如果您还要打包浏览器,则可能必须使用便携式浏览器。

您可以将便携式浏览器作为数据添加到py2exe包中,并在初始化webdriver时指定realative路径。

您可以使用以下类中的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)  

**我没有选择添加评论,所以写作答案。

答案 1 :(得分:1)

您需要在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,
            }
    }
)

答案 2 :(得分:0)

您可能想尝试CX_Freeze,它会添加代码作为单个.exe运行所需的所有必需的包/依赖项

pip install cx_Freeze

答案 3 :(得分:0)

您可以使用pyinstallercx_freeze创建python脚本/应用程序的可执行文件。

pyinstaller命令:

pyinstaller.exe --onefile --windowed <python file name>