未找到 webdriverprefs.json - pyinstaller

3 投票
3 回答
2579 浏览
提问于 2025-04-20 04:54

我有一个用Python写的程序,它使用了selenium这个库。当我用pyinstaller把这个程序打包成exe文件时,exe文件生成得很正常。但是,当我尝试从这个应用程序打开Firefox时,出现了以下错误:

IOError: [Errno 2] No such file or directory:
'C:\\users\\mohamed\\Temp\\_MEI622\\selenium\\webdriver\\firefox\\webdriver_prefs.json'

我找到了解决办法,但对我来说并没有用:

Py2exe没有把webdriver_prefs.json复制到构建中

有没有什么好的建议?

3 个回答

-1

根据我的经验,pyinstaller 在制作可执行文件的时候总是会出一些问题。我建议你可以试试 py2exe

3

简单来说,把这些 (.json 和 .xpi) 文件当作普通文件放到你的 .spec 文件里。

这是我用过的方法(你可能需要调整一下路径):

needed_selenium_files = [(r'selenium\webdriver\firefox\webdriver_prefs.json',
                           r'C:\Anaconda\Lib\site-packages\selenium\webdriver\firefox\webdriver_prefs.json',
                          'DATA'),
                         (r'selenium\webdriver\firefox\webdriver.xpi',        
                           r'C:\Anaconda\Lib\site-packages\selenium\webdriver\firefox\webdriver.xpi',
                           'DATA')]

然后在后面:

coll = COLLECT(exe, a.binaries, needed_selenium_files, ...)

想了解更多,可以查看 PyInstaller 手册: http://pythonhosted.org/PyInstaller/#toc-class-table-of-contents 还有 http://pythonhosted.org/PyInstaller/#adding-files-to-the-bundle

4

我找到了解决办法,

在把脚本打包成exe文件的时候,不要使用--onefile这个选项,而是用--onedir。这样会生成一个文件夹,里面包含所有的文件。然后把C:\python27\lib\site-packages\selenium路径下的selenium文件夹复制到你的应用程序文件夹里,这样就能正常工作了。

撰写回答