30

I have two packages (say, dataread and datainspector) that were somehow not detected by PyInstaller. Because of this, the application terminates when the running application reaches the point where it needs to import modules from those packages.

The easiest solution would be to copy dataread and datainspector into packaged app. But this will break the intention of packaging a binary version of the application.

I've read about hidded imports and hook, and I think that both can solve the problem, but I am not sure of which one to use.

Any suggestions? PS: both these packages may contain nested directories.

Elijah
  • 1,814
  • 21
  • 27
swdev
  • 4,997
  • 8
  • 64
  • 106
  • Well, it is really your decision here. You should use your own experience to choose. I think other people's ideas that worked for them might not work for you. Test them yourself for the best results. – Anthony Pham Jan 18 '15 at 23:34
  • At the moment, my experience is very limited for both ways. But working on it currently ;) I think PyInstaller documentation is need to lay a proper ground concept for those coming new to their tech. For example I am not sure where do my packages goes.... Is it in the *.exe .. or what? Would love to have a nice sum up answer for this.. ;) – swdev Jan 19 '15 at 08:57

2 Answers2

70

Hooks are files that specify additional actions when PyInstaller finds import statements.

If you add a hook-data.py file which contains a line hiddenimports = ['_proxy', 'utils', 'defs'], PyInstaller will check it to find additional imports when it sees import data.

You have to specify the path to the hook directory via --additional-hooks-dir (this is useful if you don't want to mix your source code with compile-only files, and instead keep them in a separate directory).

The simpler solution is to use --hidden-import=modulename along with the PyInstaller script. It will add modulename as import statement silently.

Hooks are better if you want to specify which import needs what additional modules. --hidden-import is simpler as a one-shot or for debugging.

More info - https://pyinstaller.readthedocs.io/en/stable/hooks.html

Dawid Gosławski
  • 2,028
  • 1
  • 18
  • 25
  • 2
    Phew.. this is the most useful answer so far. I will use it first to try solving my problem. I will try first before asking another question. Thanks! – swdev Jan 19 '15 at 11:59
  • 2
    Where are you supposed to put this file? – Asad Saeeduddin May 21 '15 at 18:25
  • file can be anywhere but it's best to keep it in separate folder than source, if you are just experimenting then you can pass `.` (dot) to --additional-hooks-dir. I used special `pyinstaller_hooks` folder. – Dawid Gosławski Jan 10 '18 at 08:40
0

Use Auto-py-to-exe to add hindden libraries. In additional files add Rasterio folder (C:/users/Admin/anaconda3/envs/name/Lib/site-packages/rasterio) and then convert to exe.

It worked for me. if it says some other libraries are missing then add folder of that library too.

  • 2
    Welcome to SO Nikhil! Can you please expain a bit more on your answer? What is auto-py-to-exe? Can you give us a link? Where is "additional files"? How can it be accessed? Thank you! – demokritos Jan 08 '21 at 09:25
  • auto-py-to-exe is a python GUI wrapper of pyinstaller to easily creates exe without writing manually pyinstaller command here is the link https://pypi.org/project/auto-py-to-exe/ – Hack Try Mar 22 '22 at 16:49
  • Using the additional files option exposes the source code which would make the packaging of the app into an executable pointless in the first place. That option should be used for adding files such as images, text files and other resources, NOT source code – Dimo Dimchev Aug 17 '22 at 08:51