[英]Py2exe and selenium - IOError: [Errno 2] No such file or directory: '\\dist\\main.exe\\selenium\\webdriver\\firefox\\webdriver_prefs.json'
我写了一个简单的应用程序,它使用selenium来浏览页面并下载它们的源代码。 现在我想让我的应用程序Windows可执行。
我的setup.py
文件:
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'bundle_files': 1,
"dll_excludes": ['w9xpopen.exe', 'MSVCP90.dll', 'mswsock.dll', 'powrprof.dll', 'MPR.dll', 'MSVCR100.dll', 'mfc90.dll'],
'compressed': True,"includes":["selenium"],
}
},
windows = [{'script': "main.py", "icon_resources": [(1, "hacker.ico")]}],
zipfile = None
)
我的程序( main.py
)(带有setup.py
文件)位于C:\\Documents and Settings\\student\\Desktop
。 Py2exe在C:\\Documents and Settings\\student\\Desktop\\dist
构建我的exe。
我将webdriver.xpi
和webdriver_prefs.json
文件复制到C:\\Documents and Settings\\student\\Desktop\\dist\\selenium\\webdriver\\firefox\\
,但是在尝试启动我的应用程序时出现错误:
Traceback (most recent call last):
File "main.py", line 73, in <module>
File "main.py", line 58, in check_file
File "main.py", line 25, in try_to_log_in
File "selenium\webdriver\firefox\webdriver.pyo", line 47, in __init__
File "selenium\webdriver\firefox\firefox_profile.pyo", line 63, in __init__
IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\student\\Desktop\\dist\\main.exe\\selenium\\webdriver\\firefox\\webdriver_prefs.json'
怎么解决这个?
实际上,它使用了这样的setup.py
文件:
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
wd_path = 'C:\\Python27\\Lib\\site-packages\\selenium\\webdriver'
required_data_files = [('selenium/webdriver/firefox',
['{}\\firefox\\webdriver.xpi'.format(wd_path), '{}\\firefox\\webdriver_prefs.json'.format(wd_path)])]
setup(
windows = [{'script': "main.py", "icon_resources": [(1, "hacker.ico")]}],
data_files = required_data_files,
options = {
"py2exe":{
"skip_archive": True,
}
}
)
但问题是我需要构建SINGLE可执行文件。
你有没有试过看看这个答案为“bundle_files = 1”的问题? 它帮助我解决了这个具体问题。
TL; DR - 请查看我构建的这个工具: https : //github.com/douglasmccormickjr/PyInstaller-Assistance-Tools--PAT
可能我建议使用PyInstaller而不是py2exe或其他任何东西,因为PyInstaller在捆绑单个可执行文件方面做得更好。 我使用python编码大约90%的时间在Windows上(没有投诉) - PyInstaller是一种比py2exe更好的选择(至少对我来说 - 我已经使用/测试了大量的Windows编译器)过去取得了不同的成功)。 也许其他遭受编译问题的人也可以从这种方法中受益。
下载我的PyInstaller-Assitance-Tools - PAT (它只有2个批处理文件和1个可执行文件,包含可执行文件的源python文件 - 对于偏执狂)...文件如上所列:
将名为“pyi-fixspec.exe”的exectuable文件放在上面提到的上一个“Scripts”文件夹中 ...这使编译更容易从长远来看!
我使用一个标准函数来引用我的python应用程序在执行/操作时需要使用的应用程序/脚本的位置 。 当app是独立的python脚本或通过pyinstaller完全编译时,此函数可以运行。
这是我使用的代码片段......
def resource_path(relative_path): """ Get absolute path to resource, works for dev and for PyInstaller """ if hasattr(sys, '_MEIPASS'): return os.path.join(sys._MEIPASS, relative_path) return os.path.join(os.path.abspath("."), relative_path)
这是我的应用程序使用它....
source = resource_path("data\\my_archive_file.zip")
这意味着app / files在目录结构方面看起来像这样:
C:\\Path\\To\\Application\\myscript_001.py <--- main application/script intended to be compiled ... C:\\Path\\To\\Application\\data\\my_archive_file.zip <---| C:\\Path\\To\\Application\\data\\images\\my_picture.jpg <---| supporting files in the bundled app C:\\Path\\To\\Application\\info\\other_stuff.json <---| ...
请注意我正在为我的应用程序捆绑的数据/文件/文件夹位于我将要编译的主要可执行文件/脚本之下...函数中的“_MEIPASS”部分让pyinstaller知道它正在作为已编译的应用程序工作... 非常重要注意:请使用“resource_path”函数,因为“pyi-fixspec.exe”应用程序将在解析/更正python应用程序路径时查找该短语/函数
转到包含上述2个批处理文件的目录 ,然后键入:
选项1 C:\\MyComputer\\Downloads\\PAT> Create_Single_Executable_with_NO_CONSOLE.bat C:\\Path\\to\\the\\python\\file.py
双击时,输出可执行文件会生成GUI应用程序
选项#2 C:\\MyComputer\\Downloads\\PAT> Create_Single_Executable_with_CONSOLE.bat C:\\Path\\to\\the\\python\\file.py
双击时,输出可执行文件会生成WINDOWS CONSOLE应用程序 - 仅需要命令行活动
您的新单文件可执行文件已完成! 在这个位置检查它
C:\\Original\\Directory\\ApplicationDistribution64bit\\NameOfPythonFile\\dist
如果您编辑/更改之前编译过的原始python文件,请在下次编译启动之前删除** \\ NameOfPythonFile **的文件夹/内容(您将要删除历史/工件文件)
喝咖啡休息时间 - 或者如果您想编辑/添加ICONS到可执行文件(以及其他项目),请查看生成的“.spec”文件和PyInstaller文档以获取配置详细信息。 您只需要在Windows控制台中再次启动它:
pyi-build.exe C:\\path\\to\\the\\pythonfile.spec
您可以使用Nuitka构建一个可以本机运行的可执行文件。 它将Python代码转换为C ++,然后编译它。
但是,它确实需要安装编译器。 适用于Microsoft Visual C ++或GCC的版本。 Microsoft发布了“用于Python 2.7的Microsoft Visual C ++编译器”,可从https://www.microsoft.com/en-us/download/details.aspx?id=44266获取 。
可以在https://github.com/develersrl/gccwinbinaries找到一个很好的Windows MinGW GCC安装程序,其中包含详细说明,包括与哪个版本的Python链接的MSVCRTXX.dll版本。
您的应用程序也将获得一点速度提升。 我写了一篇关于此类事情的博文(我不会通过点击或广告收到钱)。
https://jaredfields83.wordpress.com/2015/12/21/squeezing-more-juice-from-python/ 。
您遇到的问题是selenium试图以与py2exe不直接兼容的方式打开文件。
正如您在此处第63行所见,selenium必须打开通常随软件包一起提供的首选项文件。 它使用__file__
变量来查找它,这与py2exe不兼容 。 正如您所发现的那样,通过在您的发行版中打包prefs文件,可以解决这个问题。 但是,现在不止一个文件。
然后py2exe wiki有一个使用NSIS的方法 ,它将构建完整发行版的自解压可执行文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.