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:
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.
To find these hidden imports, build the app with the --debug=imports flag (see Getting Python's Verbose Imports above) and run it.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With