Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any pyinstaller detailed example about hidden import for psutil?

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
like image 270
coanor Avatar asked Jul 07 '15 06:07

coanor


People also ask

How do I use PyInstaller hidden import?

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.

Where is hidden import in PyInstaller?

To find these hidden imports, build the app with the --debug=imports flag (see Getting Python's Verbose Imports above) and run it.

Does PyInstaller include imports?

Analysis: Finding the Files Your Program Needs To find out, PyInstaller finds all the import statements in your script. It finds the imported modules and looks in them for import statements, and so on recursively, until it has a complete list of modules your script may use.

Does PyInstaller include libraries?

spec extension. The . Spec file has the same name as the python script file. PyInstaller creates a distribution directory, DIST containing the main executable and the dynamic libraries bundled in an executable file.


1 Answers

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

like image 100
nemo Avatar answered Sep 29 '22 01:09

nemo