使用cx_Freeze或py2exe发行打包scrapy spider [英] Issue packaging scrapy spider with cx_Freeze or py2exe
问题描述
我已经使用Scrapy和wxPython创建了一个抓取工具,该抓取工具可以按预期工作,并以CSV格式将包含结果的文件导出到桌面。我正在尝试使用以下命令行将其打包为带有cx_Freeze的可执行文件:
I've created a scraper with Scrapy and wxPython which works as expected, exporting a file with results to the desktop in CSV format. I'm attempting to package this into an executable with cx_Freeze using the below command prompt line:
cxfreeze ItemStatusChecker.py --target-dir dist
这似乎很好用,使用ItemStatusChecker.exe构建dist目录
This seems to work fine, building the dist directory with ItemStatusChecker.exe
但是,当我打开ItemStatusChecker.exe时,在命令提示符下出现以下错误,并且我的GUI无法启动:
However, when I open ItemStatusChecker.exe, I get the below error in the command prompt and my GUI does not launch:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec code in m.__dict__
File "ItemStatusChecker.py", line 6, in <module>
File "C:\Python27\lib\site-packages\scrapy\__init__.py", line 6, in <module>
__version__ = pkgutil.get_data(__package__, 'VERSION').strip()
File "C:\Python27\lib\pkgutil.py", line 591, in get_data
return loader.get_data(resource_name_)
IOError: [Errno 2] No such file or directory: 'scrapy\\VERSION'
我也尝试通过py2exe运行它。创建dist目录似乎也可以正常工作,但是尝试启动exe时出现类似错误:
I've tried running it through py2exe as well. This also seems to work fine, creating the dist directory, but I get a very similar error when trying to launch the exe:
Traceback (most recent call last):
File "ItemStatusChecker.py", line 6, in <module>
File "scrapy\__init__.pyc", line 6, in <module>
File "pkgutil.pyc", line 591, in get_data
IOError: [Errno 2] No such file or directory: 'scrapy\\VERSION'
我是python新手,所以让我知道是否遗漏了任何必要的细节。预先感谢您提供任何建议!
I'm a python newbie, so let me know if I left out any necessary details. Thank you in advance for any advice you can offer!
推荐答案
请看一下( http://cx-freeze.readthedocs.org/en/latest/faq.html#using-数据文件)
我使用cx-freeze的解决方案:
My solution using cx-freeze:
修改文件 C:\Python2.7\Lib\站点软件包\scrapy__init __。py
import sys
# Scrapy version
import pkgutil
if getattr(sys, 'frozen', False):
__version__ = 'VERSION'.decode('ascii').strip()
else:
__version__ = pkgutil.get_data(__package__, 'VERSION').decode('ascii').strip()
和文件 C:\Python2.7\Lib\site-packages\scrapy\responsetypes.py
import sys
from mimetypes import MimeTypes
from pkgutil import get_data
...
def __init__(self):
self.classes = {}
self.mimetypes = MimeTypes()
if getattr(sys, 'frozen', False):
mimedata = 'mime.types'
else:
mimedata = get_data('scrapy', 'mime.types')
self.mimetypes.readfp(StringIO(mimedata))
并在setup.py中包含此生成exe选项
And in setup.py include this build exe option
"include_files": [("C:\\Python27\\Lib\\site-packages\\scrapy\\VERSION","VERSION"),
("C:\\Python27\\Lib\\site-packages\\scrapy\\mime.types","mime.types")]}
这篇关于使用cx_Freeze或py2exe发行打包scrapy spider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!