python scrapy conversion to exe file using pyinstaller

前端 未结 1 572
抹茶落季
抹茶落季 2021-01-22 13:54

I am trying to convert a scrapy script to a exe file. The main.py file looks like this:

from scrapy.crawler import CrawlerProcess
from amazon.spiders.amazon_scr         


        
相关标签:
1条回答
  • 2021-01-22 14:40

    I had the same situation.
    Instead of trying to make pyinstaller count this file (I failed all my attempts to do it) I decided to check and change some part of scrapy code in order to avoid this error.

    I noticed that there is only one place where \scrapy\VERSION file used-- \scrapy\__init__.py
    I decided to hardcode that value from scrapy\version by changing scrapy__init__.py :

    #import pkgutil
    __version__ = "1.5.0" #pkgutil.get_data(__package__, 'VERSION').decode('ascii').strip()
    version_info = tuple(int(v) if v.isdigit() else v
                         for v in __version__.split('.'))
    #del pkgutil
    

    After this change there is no need to store version in external file. As there is no reference to \scrapy\version file - that error will not occure.

    After that I had the same FileNotFoundError: [Errno 2] with \scrapy\mime.types file.
    There is the same situation with \scrapy\mime.types - it used only in \scrapy\responsetypes.py

    ...
    #from pkgutil import get_data
    ...
        def __init__(self):
            self.classes = {}
            self.mimetypes = MimeTypes()
            #mimedata = get_data('scrapy', 'mime.types').decode('utf8')
            mimedata = """
            Copypaste all 750 lines of \scrapy\mime.types here
    """
            self.mimetypes.readfp(StringIO(mimedata))
            for mimetype, cls in six.iteritems(self.CLASSES):
                self.classes[mimetype] = load_object(cls)
    

    This change resolved FileNotFoundError: [Errno 2] with \scrapy\mime.types file. I agree that hardcode 750 lines of text into python code is not the best decision.

    After that I started to recieve ModuleNotFoundError: No module named scrapy.spiderloader . I added "scrapy.spiderloader" into hidden imports parameter of pyinstaller.
    Next Issue ModuleNotFoundError: No module named scrapy.statscollectors.
    Final version of pyinstaller command for my scrapy script consist of 46 hidden imports - after that I received working .exe file.

    0 讨论(0)
提交回复
热议问题