7

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
nemo
  • 12,241
  • 3
  • 21
  • 26
coanor
  • 3,746
  • 4
  • 50
  • 67

4 Answers4

6

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

nemo
  • 12,241
  • 3
  • 21
  • 26
  • EDIT: the above solution is tested under Ubuntu 14.04. To make it work on windows, you probably need to include something like `_psutile_windows`. – nemo Jul 24 '15 at 13:44
  • 1
    For pyinstaller 3 you must use the following code to import **collect_submodules** : `from PyInstaller.utils.hooks import collect_submodules` [PyInstaller Documentation](https://pythonhosted.org/PyInstaller/#useful-items-in-pyinstaller-utils-hooks) – Etienne Prothon Nov 27 '15 at 07:29
0

pyinstall is hard to configure, the cx_freeze maybe better, both support windows (you can download the exe directly) and linux. Provide the example.py, In windows, suppose you have install python in the default path (C:\\Python27):

$ python c:\\Python27\\Scripts\\cxfreeze example.py -s --target-dir some_path

the cxfreeze is a python script, you should run it with python, then the build files are under some_path (with a lot of xxx.pyd and xxx.dll).

In Linux, just run:

$ cxfreeze example.py -s --target-dir some_path

and also output a lot of files(xxx.so) under some_path.

The defect of cx_freeze is it would not wrap all libraries to target dir, this means you have to test your build under different environments. If any library missing, just copy them to target dir. A exception case is, for example, if your build your python under Centos 6, but when running under Centos 7, the missing of libc.so.6 will throw, you should compile your python both under Centos 7 and Centos 6.

coanor
  • 3,746
  • 4
  • 50
  • 67
0

What worked for me is as follows:

  1. Install python-psutil: sudo apt-get install python-psutil. If you have a previous installation of the psutil module from other method, for example through source or easy_install, remove it first.

  2. Run pyinstaller as you do, without the hidden-import option.

MervS
  • 5,724
  • 3
  • 23
  • 37
-1

still facing the error Implementation: 1.python program with modules like platform , os , shutil and psutil when i run the script directly using python its working fine.

2.if i build a binary using pyinstaller. The binary is build successfully. But if i run the binary iam getting the No module named psutil found.I had tried several methods like adding the hidden import and other things. None is working. I trying it almost 2 to 3 days. Error: ModuleNotFoundError: No module named 'psutil' Command used for the creating binary pyinstaller --hidden-import=['_psutil_linux'] --onefile --clean serverHW.py

i tried --additional-hooks-dir= also not working. When i run the binary im getting module not found error.

  • You should improve at least the formatting of your answer. I could also do it, but I can't understand what you want to say with your answer. Is a question? Are you suggesting a solution? – Danny_DD Jan 07 '21 at 07:31