捆绑一个独立的exe
Bundling a standalone exe
所以,很长一段时间以来,我一直在努力制作 Standalone EXE
。现在我安装了 python 32 位,我尝试 py2exe
,使用它的 bundle_files
选项为我的项目 comic-dl 制作一个 exe。
由于 comic-dl 有点像 youtube-dl(甚至是该死的名字),所以在我无法让自己的 setup.p 文件正常工作后,我复制了 youtube-dls' setup.py file然后我修改它以使其适用于我的项目。
Here is my modification。然后我 运行 这些命令:
python setup.py install
python setup.py py2exe
一切顺利,我得到了一个 comic-dl.exe
(~6 MB)。但是,当我执行它时,出现此错误:
Traceback (most recent call last):
File "comic-dl.py", line 4, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "honcho.pyo", line 12, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "sites\mangafox.pyo", line 13, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "selenium\webdriver\__init__.pyo", line 18, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "selenium\webdriver\firefox\webdriver.pyo", line 39, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "selenium\webdriver\remote\webdriver.pyo", line 25, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "selenium\webdriver\remote\webelement.pyo", line 40, in <module>
File "pkgutil.pyo", line 591, in get_data
IOError: [Errno 2] No such file or directory: 'selenium\webdriver\remote\getAttribute.js'
我检查了 selenium 文件夹中的 getAttribute.js
文件,该文件就在那里。我什至在项目本身复制了 selenium 的文件夹,仍然没有变化。
然后我尝试了 this 但这没有意义,因为并非所有内容都捆绑在选项 3 中,当我更改选项并执行设置命令时,选项 2 对我来说似乎相同。
在这种情况下如何获得独立的 exe?
看起来您可能必须在相对路径中设置可执行文件才能到达硬编码文件位置。
或者,我建议您使用 PyInstaller,在最好的情况下,它不需要任何配置,并且可以在一个命令中立即为您提供一个可运行的可执行文件。
这是我的解决方案。希望能帮到你!
第一步:修改文件webelement.py
# customized code to resolve resources's path problem when executing py2exe executables
import sys
frozen = getattr(sys, 'frozen', '')
if not frozen:
getAttribute_js = pkgutil.get_data(__package__, 'getAttribute.js').decode('utf8')
isDisplayed_js = pkgutil.get_data(__package__, 'isDisplayed.js').decode('utf8')
else:
approot = os.path.dirname(sys.executable)
getAttribute_js = open(os.path.join(approot, 'getAttribute.js'), 'rb').read().decode('utf8')
isDisplayed_js = open(os.path.join(approot, 'isDisplayed.js'), 'rb').read().decode('utf8')
注意:在我的环境中,此文件位于 C:\Python27\Lib\site-packages\selenium\webdriver\remote。
第 2 步:在您的 setup.py for py2exe 中,将所需的文件包含到您的数据文件中。例如
data_files = [(r'.', glob(r'C:\Python27\Lib\site-packages\selenium\webdriver\remote\getAttribute.js')),
(r'.', glob(r'C:\Python27\Lib\site-packages\selenium\webdriver\remote\isDisplayed.js'))]
然后你应该让你的 exe 工作。我已经在 windows 7、32 位系统中对此进行了测试。 python 2.7,硒 3.0.2。
我也遇到了这个问题。这是我解决它的方法:
- Goto
C:\Python27\selenium\webdriver\remote\
[如果selenium安装在其他目录,目录可能不同。本质是找到文件]。有 getAttribute.js
和 IsDisplayed.js
个文件。复制它们。
- 在您的分发
dist
文件夹中,有一个 library.zip
文件。解压缩或在 Windows 默认查看器中打开它。
- 转到
selenium\webdriver\remote\
并粘贴在步骤 1 中复制的文件。
- 如果您解压缩
library.zip
,请压缩并替换原来的。
它对我有用。希望这可以帮助你。
为我工作
- C:\Python27\selenium\webdriver\remote\ 有getAttribute.js和IsDisplayed.js个文件。复制它们。
- 将它们粘贴到 dist\selenium\webdriver\remote 中,而不是粘贴到 zip 文件中。
- 运行.exe
- 利润
所以,很长一段时间以来,我一直在努力制作 Standalone EXE
。现在我安装了 python 32 位,我尝试 py2exe
,使用它的 bundle_files
选项为我的项目 comic-dl 制作一个 exe。
由于 comic-dl 有点像 youtube-dl(甚至是该死的名字),所以在我无法让自己的 setup.p 文件正常工作后,我复制了 youtube-dls' setup.py file然后我修改它以使其适用于我的项目。
Here is my modification。然后我 运行 这些命令:
python setup.py install
python setup.py py2exe
一切顺利,我得到了一个 comic-dl.exe
(~6 MB)。但是,当我执行它时,出现此错误:
Traceback (most recent call last):
File "comic-dl.py", line 4, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "honcho.pyo", line 12, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "sites\mangafox.pyo", line 13, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "selenium\webdriver\__init__.pyo", line 18, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "selenium\webdriver\firefox\webdriver.pyo", line 39, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "selenium\webdriver\remote\webdriver.pyo", line 25, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "selenium\webdriver\remote\webelement.pyo", line 40, in <module>
File "pkgutil.pyo", line 591, in get_data
IOError: [Errno 2] No such file or directory: 'selenium\webdriver\remote\getAttribute.js'
我检查了 selenium 文件夹中的 getAttribute.js
文件,该文件就在那里。我什至在项目本身复制了 selenium 的文件夹,仍然没有变化。
然后我尝试了 this 但这没有意义,因为并非所有内容都捆绑在选项 3 中,当我更改选项并执行设置命令时,选项 2 对我来说似乎相同。
在这种情况下如何获得独立的 exe?
看起来您可能必须在相对路径中设置可执行文件才能到达硬编码文件位置。
或者,我建议您使用 PyInstaller,在最好的情况下,它不需要任何配置,并且可以在一个命令中立即为您提供一个可运行的可执行文件。
这是我的解决方案。希望能帮到你!
第一步:修改文件webelement.py
# customized code to resolve resources's path problem when executing py2exe executables
import sys
frozen = getattr(sys, 'frozen', '')
if not frozen:
getAttribute_js = pkgutil.get_data(__package__, 'getAttribute.js').decode('utf8')
isDisplayed_js = pkgutil.get_data(__package__, 'isDisplayed.js').decode('utf8')
else:
approot = os.path.dirname(sys.executable)
getAttribute_js = open(os.path.join(approot, 'getAttribute.js'), 'rb').read().decode('utf8')
isDisplayed_js = open(os.path.join(approot, 'isDisplayed.js'), 'rb').read().decode('utf8')
注意:在我的环境中,此文件位于 C:\Python27\Lib\site-packages\selenium\webdriver\remote。
第 2 步:在您的 setup.py for py2exe 中,将所需的文件包含到您的数据文件中。例如
data_files = [(r'.', glob(r'C:\Python27\Lib\site-packages\selenium\webdriver\remote\getAttribute.js')),
(r'.', glob(r'C:\Python27\Lib\site-packages\selenium\webdriver\remote\isDisplayed.js'))]
然后你应该让你的 exe 工作。我已经在 windows 7、32 位系统中对此进行了测试。 python 2.7,硒 3.0.2。
我也遇到了这个问题。这是我解决它的方法:
- Goto
C:\Python27\selenium\webdriver\remote\
[如果selenium安装在其他目录,目录可能不同。本质是找到文件]。有getAttribute.js
和IsDisplayed.js
个文件。复制它们。 - 在您的分发
dist
文件夹中,有一个library.zip
文件。解压缩或在 Windows 默认查看器中打开它。 - 转到
selenium\webdriver\remote\
并粘贴在步骤 1 中复制的文件。 - 如果您解压缩
library.zip
,请压缩并替换原来的。
它对我有用。希望这可以帮助你。
为我工作
- C:\Python27\selenium\webdriver\remote\ 有getAttribute.js和IsDisplayed.js个文件。复制它们。
- 将它们粘贴到 dist\selenium\webdriver\remote 中,而不是粘贴到 zip 文件中。
- 运行.exe
- 利润