使用cx_Freeze编译为exe时,没有这样的文件或目录webdriver_prefs.json

时间:2015-09-23 11:59:18

标签: python firefox selenium cx-freeze

我使用selenium firefox webdriver写了一个应用程序,并用cx_Freeze编译它。当我启动我的应用程序时,我收到一个错误:

Traceback (most recent call last):
  File "c:\111\ui\__init__.py", line 27, in login
    self.browser = self.webdriver.Firefox()
  File "C:\Python34\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 47, in __init__
    self.profile = FirefoxProfile()
  File "C:\Python34\lib\site-packages\selenium\webdriver\firefox\firefox_profile.py", line 63, in __init__
    WEBDRIVER_PREFERENCES)) as default_prefs:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\111\\build\\exe.win32-3.4\\library.zip\\selenium\\webdriver\\firefox\\webdriver_prefs.json'

但我的library.zip实际上包含webdriver_prefs.json和webdriver.xpi。我使用下一个setup.py文件来添加它:

import sys
from cx_Freeze import setup, Executable

base= 'C:\\Python34\\Lib\\site-packages\\selenium\\webdriver'

includes = [
    ('%s\\firefox\\webdriver.xpi' %(base), 'selenium/webdriver/firefox/webdriver.xpi'), 
    ('%s\\firefox\\webdriver_prefs.json '%(base), 'selenium/webdriver/firefox/webdriver_prefs.json')
]

build_exe_options = {
    "packages": ["os"], 
    "excludes": ["tkinter"],
    "zip_includes": includes,
}

setup(
    name = "lala", 
    version = "0.1", 
    description = "lalala", 
    options = {"build_exe": build_exe_options}, 
    executables = [Executable("app.py", base=base)],
)

Shoud我以某种方式为我的可执行文件注册了这些文件?为什么traceback用两种方式打印文件路径(一个反斜杠和两个反斜杠)?

1 个答案:

答案 0 :(得分:1)

最后我无法用cx_Freeze来解决问题,但后来我尝试了PyInstaller,它就像一个魅力!它已经支持Python3了。我用了那个命令:

c:\Python34\Scripts\pyinstaller.exe -p C:\Python34\Lib\site-packages -F app.py