Any pyinstaller detailed example about hidden import for psutil?

coanor picture coanor · Jul 7, 2015 · Viewed 10.8k times · Source

I want to compile my python code to binary by using pyinstaller, but the hidden import block me. For example, the following code import psutil and print the CPU count:

# example.py
import psutil
print psutil.cpu_count()

And I compile the code:

$ pyinstaller -F example.py --hidden-import=psutil

When I run the output under dist:

ImportError: cannot import name _psutil_linux

Then I tried:

$ pyinstaller -F example.py --hidden-import=_psutil_linux

Still the same error. I have read the pyinstall manual, but I still don't know how to use the hidden import. Is there a detailed example for this? Or at least a example to compile and run my example.py?

ENVs:

  • OS: Ubuntu 14.04
  • Python: 2.7.6
  • pyinstaller: 2.1

Answer

nemo picture nemo · Jul 24, 2015

Hi hope you're still looking for an answer. Here is how I solved it:

add a file called hook-psutil.py

from PyInstaller.hooks.hookutils import (collect_data_files, collect_submodules)

datas = [('./venv/lib/python2.7/site-packages/psutil/_psutil_linux.so', 'psutil'),
         ('./venv/lib/python2.7/site-packages/psutil/_psutil_posix.so', 'psutil')]
hiddenimports = collect_submodules('psutil')

And then call pyinstaller --additional-hooks-dir=(the dir contain the above script) script.py