0

I have been trying to build an executable from a python script with pyinstaller, but, once I run it with

pyinstaller --onefile stock_visual.py

in the windows cmd, and generates the files, whenever I run it, I get the following error:

Traceback (most recent call last):
  File "stock_visual.py", line 6, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "c:\users\verdi\github\jiostocks\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "lib\site-packages\dash_core_components\__init__.py", line 12, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Verdi\\AppData\\Local\\Temp\\_MEI112322\\dash_core_components\\package-info.json'
[4824] Failed to execute script stock_visual

Now, I have seen several people encountering a similar problem, but they were due to the need of importing manually a dynamically imported library like explained in the pyinstaller documentation here:

https://pythonhosted.org/PyInstaller/when-things-go-wrong.html#listing-hidden-imports

This did not seem to work in my case, and the directory it is looking for the dash_core_components files is not even created: Folder screenshot

As a side note, if i do not use the --onefile option, the same problem occurs but the folder is INSIDE the project, in the \build folder, but the directory the program looks for the library does not exist either.

By now, I have tried the --onedir option, including the libraries manually as I said before, everything said in the pyinstaller troubleshooting web page basically and the py2exe and cx freezer alternatives, which also gave their own errors.

The script I am trying to use the builder with can be found in this repo and I have tried it with other scripts and runs without any problem.

I am using windows10 64 bits, with python3.8 and a closed virtual environment (venv) with all the libraries listed in the requirements.txt file contained in said repository.

Any hint of how could I fix this problem would be very much appreciated.

  • 1
    Please don't use that doc link. It hasn't been updated in 4 years. Use PyInstaller.readthedocs.io instead. – Legorooj Mar 02 '20 at 22:42
  • @Verdi Esteban Rey Blanco can you kindly share what was the command you used with pyinstaller? With your venv, did you have to point your `paths` to `site-packages`? – lagrangian_headache Jan 06 '21 at 16:52

1 Answers1

1

You'll need to create a hook file for the dash_*** module. The following will probably work:

# hooks/hook-dash_core_components.py

from PyInstaller.utils.hooks import collect_all

datas, binaries, hiddenimports = collect_all('dash_core_components')

Save that in a file called hook-dash_core_components.py under a directory that is next to your file:

- myfile.py
- hooks
  - hook-dash_***.py

And add an option when compiling; --additional-hook-dir=hooks.

Legorooj
  • 2,533
  • 2
  • 14
  • 31
  • I encountered same problem as original poster and this solution works for me. Strange, I'd have thought that if we had installed the module in the virtual environment, and added `--paths=venv\Lib\site-packages`, all relevant packages would have been copied into the `dist` folder. – lagrangian_headache Jan 06 '21 at 14:16
  • The default module path is already read and searched by PyInstaller. However PyInstaller only copies the actual module code by default - so datafiles, metadata, and some binary files (DLLs/SO) aren't copied by default. – Legorooj Jan 07 '21 at 04:23
  • Ah, I see thanks. When should we use hooks in this format, vs what you've shared in another thread? https://stackoverflow.com/questions/60384288/pyinstaller-modulenotfounderror I believe the size of output directory expands quite fast as additional hooks are added. – lagrangian_headache Jan 07 '21 at 05:44
  • I'm afraid I don't understand the question properly. – Legorooj Jan 07 '21 at 07:10
  • I apologize. In one answer, you've recommended creating the hook file by assigning `datas, binaries, hiddenimports` to a function, while elsewhere, you defined a `hook` function and iterated through packages. I was wondering when we should choose either approach. Additionally, my output .exe (using `--onefile`) expands quickly in size as I create more hook files. I have them created for `dash_core_components`, `dash_html_components`, `dash_renderer`, `flask_compress`, and `plotly`. Resultantly, the exe takes long to run. I know that `--onedir` speeds up loading though. – lagrangian_headache Jan 07 '21 at 09:44