Any pyinstaller detailed example about hidden import for psutil?
Asked Answered
A

4

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
Angle answered 7/7, 2015 at 6:44 Comment(2)
why down vote this?Angle
Probably for not giving the versions of python and pyinstaller you are using.Neustria
V
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

Victoir answered 24/7, 2015 at 13:28 Comment(2)
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.Victoir
For pyinstaller 3 you must use the following code to import collect_submodules : from PyInstaller.utils.hooks import collect_submodules PyInstaller DocumentationMicrodot
A
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.

Angle answered 9/7, 2015 at 9:14 Comment(0)
Z
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.

Zingg answered 11/8, 2015 at 7:59 Comment(0)
S
-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.

Stretto answered 7/1, 2021 at 4:35 Comment(1)
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?Johathan

© 2022 - 2024 — McMap. All rights reserved.