I have a python script that uses scrapy and I want to make it into an exe file using pyinstaller. The exe file is generated without any error but when I open it an error occurs.
FileNotFoundError: [Errno 2] No such file or directory: '...\\scrapy\\VERSION'
I have tried reinstalling scrapy but that did not help. I am using windows 10 with python3
Full Disclosure: This is a repost of my answer to a duplicate, similar question. I am just putting it here for visiblity. This genuinely answers the question asked; so it is relevant.
You did not use Pyinstaller properly when you had built your stand-alone program. Here is a short, layman's description of how Pyinstaller works: Pyinstaller bundles the Python interpreter, necessary DLLs (for Windows), your project's source code, and all the modules it can find into a folder or self-extracting executable. Pyinstaller does not include modules or files it cannot find in the final .exe (Windows), .app (macOS), folder, etc. that results when you run Pyinstaller.
So, here is what happened:
You ran your frozen/stand-alone program. As soon as you did this, your program was 'extracted' to a new, temporary folder on your computer
/temp/_MEIbxALM3/
. This folder contains the Python interpreter, your program's source code, and the modules Pyinstaller managed to find (plus a couple other necessary files).The Scrapy module is more than just a module. It is an entire framework. It has its own plain text files (besides Python files) that it uses. And, it imports a lot of modules itself.
The Scrapy framework especially does not get along with Pyinstaller because it uses many methods to import modules that Pyinstaller cannot 'see'. Also, Pyinstaller basically makes no attempt to include files in the final build that are not .py files unless you tell it to.
So, what really happened?
The text file 'VERSION' that exists in the 'normal' scrapy module on your computer (that you had installed with pip or pipenv) was not included in the copycat scrapy module in the build of your program. Scrapy needs this file; Python is giving you the
FileNotFoundError
because it simply was never included. So, you have to include the file in the build of your program with Pyinstaller.How do you tell Pyinstaller where to find modules and files?
This guy says to just copy the missing files from where they are installed on your computer into your build folder spit out from Pyinstaller. This does work. But, there is a better way and Pyinstaller can do more of the work for you (preventing further
ImportError
s andFileNotFoundError
s you may get). See below:build.spec
Files are Your Friendspec
files are just Python files that Pyinstaller uses like a configuration file to tell it how to build your program. Read more about them here. Below is an example of a realbuild.spec
file I used recently to build a Scrapy program with a GUI for Windows (my project's name is B.O.T. Bot):Uncomment the last region if you want to build a folder instead of a stand-alone
.exe
. This is a configuration file specific to my computer and project structure. So in your file, you would have to change a few things (for examplepathex
to tell Pyinstaller where to find DLLs on Windows 10. But, the premise is the same.My project directory looks like this:
Pay special attention to the
hooks/
directory. Using hooks will save you from a lot of headaches down the road. Read more about Pyinstaller's hooks feature here. In thehooks/
directory there is a hook file for Scrapy. This will tell Pyinstaller to include many modules and files it would have otherwise missed if you did not use a.spec
file. This is the most important thing I have wrote here so far. If you do not do this step, you will keep gettingImportError
s every time you try to run a Scrapy program built using Pyinstaller. Scrapy imports MANY modules that Pyinstaller misses.hook-scrapy.py
(Note: Your hook file must be named just like this.):After you finished writing a proper
build.spec
file, all you need to do is run Pyinstaller like this in your shell prompt:Pyinstaller should then spit out a proper build of your program that should work. Problem solved.
Those Google'ing for an answer to this problem or really any issue with Pyinstaller or Scrapy, pray you find my answer.
You can find that file in scrapy package. Go to this Path: Python/Lib/site-packages/scrapy, and you will find that file. Here are the steps that you are gonna do next:
Good luck. :)