简体   繁体   English

任何关于 psutil 隐藏导入的 pyinstaller 详细示例?

[英]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.我想使用pyinstaller将我的 python 代码编译为二进制文件,但hidden import阻止了我。 For example, the following code import psutil and print the CPU count:例如,以下代码导入psutil并打印 CPU 计数:

# 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 :当我在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 .我已经阅读了 pyinstall 手册,但我仍然不知道如何使用hidden import Is there a detailed example for this?有详细的例子吗? Or at least a example to compile and run my example.py ?或者至少是一个编译和运行我的example.py的例子?

ENVs: ENV:

  • OS: Ubuntu 14.04操作系统:Ubuntu 14.04
  • Python: 2.7.6蟒蛇:2.7.6
  • pyinstaller: 2.1 py安装程序:2.1

Hi hope you're still looking for an answer.您好,希望您仍在寻找答案。 Here is how I solved it:这是我解决它的方法:

add a file called hook-psutil.py添加一个名为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然后调用pyinstaller --additional-hooks-dir=(the dir contain the above script) script.py

pyinstall is hard to configure, the cx_freeze maybe better, both support windows (you can download the exe directly) and linux. pyinstall很难配置, cx_freeze可能更好,都支持windows(可以直接下载exe )和linux。 Provide the example.py , In windows, suppose you have install python in the default path ( C:\\\\Python27 ):提供example.py ,在 windows 中,假设您已经在默认路径 ( C:\\\\Python27 ) 中安装了 python:

$ 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 ). cxfreeze是一个 python 脚本,你应该用python运行它,然后构建文件在some_path下(有很多xxx.pydxxx.dll )。

In Linux, just run:在 Linux 中,只需运行:

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

and also output a lot of files( xxx.so ) under some_path .并在some_path下输出很多文件( xxx.so )。

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. cx_freeze的缺陷是它不会将所有库都包装到目标目录,这意味着您必须在不同的环境下测试您的构建。 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.一个例外的情况是,例如,如果您在 Centos 6 下构建您的 python,但在 Centos 7 下运行时,会抛出 libc.so.6 丢失,您应该在 Centos 7 和 Centos 6 下编译您的 python。

What worked for me is as follows:对我有用的是:

  1. Install python-psutil: sudo apt-get install python-psutil .安装 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.如果您以前通过其他方法安装了psutil模块,例如通过 source 或easy_install ,请先将其删除。

  2. Run pyinstaller as you do, without the hidden-import option.像你一样运行pyinstaller ,没有hidden-import选项。

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.仍然面临错误 Implementation: 1.python program with modules like platform , os ,shutil 和 psutil 当我直接使用 python 运行脚本时,它工作正常。

2.if i build a binary using pyinstaller. 2.如果我使用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.但是如果我运行二进制文件,我会发现没有名为 psutil 的模块。我尝试了几种方法,比如添加隐藏的导入和其他东西。 None is working.没有一个在工作。 I trying it almost 2 to 3 days.我尝试了将近 2 到 3 天。 Error: ModuleNotFoundError: No module named 'psutil' Command used for the creating binary pyinstaller --hidden-import=['_psutil_linux'] --onefile --clean serverHW.py错误: ModuleNotFoundError: No module named 'psutil'命令用于创建二进制pyinstaller --hidden-import=['_psutil_linux'] --onefile --clean serverHW.py

i tried --additional-hooks-dir= also not working.我试过 --additional-hooks-dir= 也不起作用。 When i run the binary im getting module not found error.当我运行二进制文件时,我正在获取模块未找到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM