简体   繁体   English

使用 PyInstaller 将 Cython 编译的模块和 python 代码构建为可执行二进制文件

[英]Build Cython-compiled modules and python code into executable binary using PyInstaller

I am trying to package my project code into a an executable binary using Cython and PyInstaller libraries.我正在尝试使用CythonPyInstaller库将我的项目代码打包成一个可执行二进制文件。 My code directory looks like this:我的代码目录如下所示:

代码目录 The main.py is the main code which imports the logic from program_a.py and program_b.py . main.py是从program_a.pyprogram_b.py导入逻辑的主要代码。

I am successfully able to convert my program_a and program_b files into.so files which can be imported by any python code.我成功地将我的program_aprogram_b文件转换为 .so 文件,这些文件可以由任何 python 代码导入。 I did this by executing the following script.我通过执行以下脚本来做到这一点。

from distutils.core import setup
from Cython.Build import cythonize

sourcefiles = ['program_a.py', 'program_b.py']

setup(
    name = "Hello World",
    ext_modules = cythonize(sourcefiles), 
)

By executing > python setup.py build_ext --inplace I get .so files as shown below通过执行 > python setup.py build_ext --inplace我得到.so文件,如下所示

带库的二进制文件 When I run python main.py it runs perfectly with .so files.当我运行python main.py时,它与.so文件完美运行。 Which shows that I can import them as a module.这表明我可以将它们作为模块导入。

Now, I want to package binaries (.so) files and main.py into single binary file.现在,我想将二进制 (.so) 文件和main.py成单个二进制文件。 For that I used the following command provided by pyInstaller为此,我使用了pyInstaller提供的以下命令

pyinstaller "main.py" --onefile

It actually gives a binary in dist/ folder but I cannot able to import some modules and getting the following error:它实际上在dist/文件夹中提供了一个二进制文件,但我无法导入某些模块并收到以下错误:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import program_a as lisence_checker
  File "program_a.py", line 1, in init program_a
ModuleNotFoundError: No module named 'licensing'
[18032] Failed to execute script main

How can I link libraries with the pyinstaller or embed library information into my binaries?如何将库与 pyinstaller 链接或将库信息嵌入到我的二进制文件中?

What I found yet:我发现了什么:

  1. Building Cython-compiled python code with PyInstaller 使用 PyInstaller 构建 Cython 编译的 python 代码

  2. https://riptutorial.com/cython/example/21982/bundling-a-cython-program-using-pyinstaller https://riptutorial.com/cython/example/21982/bundling-a-cython-program-using-pyinstaller

But all of these above links are not using any external package inside there python code examples.但是上面所有这些链接都没有在 python 代码示例中使用任何外部包。 I am able to compile the code without external modules我能够在没有外部模块的情况下编译代码

After getting familiar with PyInstaller package I am able to figure out the issue.在熟悉 PyInstaller 包后,我能够找出问题所在。 I followed the following steps to make it work for me at the end.最后,我按照以下步骤使它对我有用。

Now, posting my answer to help others:)现在,发布我的答案以帮助其他人:)

## Build *.so files from python modules 
    1. Execute "setup.py" file
       > python setup.py build
    2. It will generate "*.so" modules inside "build/lib.linux-x86_64-3.6" dir.

## Created binary from cython modules
    1. Copy the binaries (i.e. *.so) files into binary folder
    2. Get inside the binary folder 'cd binary'
    3. Run Pyinstaller command inside binary directory: `python -O -m PyInstaller --clean --onefile idps.spec`
    4. Your binary will be inside dist folder 'binary/dist/'
    5. Execute the binary in linux using './dist/sample_app'
    6. Your app is ready :)

Here is spec file to make it work for me:这是使它对我有用的规范文件:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['main.py'],
             pathex=['cython_pyinstaller_sample/binary'],
             binaries=[('program_a.cpython-36m-x86_64-linux-gnu.so', '.'),('program_b.cpython-36m-x86_64-linux-gnu.so', '.')],
             datas=[('config_file.txt', '.')],
             hiddenimports=['licensing', 'licensing.methods', 'pandas'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False) pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher) exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='sample_app',
          debug=True,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

Just in case someone's looking for a quick fix.以防万一有人正在寻找快速修复。

I ran into the same situation and found a quick/dirty way to do the job.我遇到了同样的情况,并找到了一种快速/肮脏的方式来完成这项工作。 The issue is that pyinstaller is not adding the necessary libraries in the.exe file that are needed to run your program.问题是 pyinstaller 没有在 .exe 文件中添加运行程序所需的必要库。

All you need to do is import all the libraries (and the.so files) needed into your main.py file (the file which calls program_a.py and program_b.py).您需要做的就是将所需的所有库(和 .so 文件)导入您的 main.py 文件(调用 program_a.py 和 program_b.py 的文件)。 For example, assume that program_a.py uses opencv library (cv2) and program_b.py uses matplotlib library.例如,假设 program_a.py 使用 opencv 库 (cv2),program_b.py 使用 matplotlib 库。 Now in your main.py file you need to import cv2 and matplotlib as well.现在在您的 main.py 文件中,您还需要导入 cv2 和 matplotlib。 Basically, whatever you import in program_a.py and program_b.py, you have to import that in main.py as well.基本上,无论您在 program_a.py 和 program_b.py 中导入什么,您也必须在 main.py 中导入它。 This tells pyinstaller that the program needed these libraries and it includes those libraries in the exe file.这告诉 pyinstaller 该程序需要这些库,并将这些库包含在 exe 文件中。

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

相关问题 使用 PyInstaller 构建 Cython 编译的 Python 代码 - Building Cython-compiled python code with PyInstaller Python 3.11 对 Cython 编译模块的影响 - Effect of Python 3.11 on Cython-compiled modules 使用Python setuptools将Cython编译的pyd文件放入其原始文件夹中? - Using Python setuptools to put Cython-compiled pyd files in their original folders? 如何使用 Visual Studio 编译器在 Windows 上编译 cython 编译的 c 代码 - How to compile cython-compiled c code on Windows using Visual Studio Compilers 在C编译的python代码上使用pyinstaller - Using pyinstaller on c compiled python code 使用 Pyinstaller 或 Cython 从 Python 模块创建可执行文件 - Create Executable from a Python module with Pyinstaller or Cython 如何使用 cython 编译 python 脚本导入模块到可执行文件 - How to compile python script importing modules to executable using cython Pyinstaller编译的可执行文件失败并显示139退出代码 - Pyinstaller compiled executable fails with 139 exit code 如何使用 PyInstaller 将 python 脚本转换为二进制可执行文件 - How to convert python scripts to binary executable using PyInstaller 无法将 Cython 文件实现为 Python 文件并使用 Pyinstaller 创建可执行文件 - Can't Implement Cython File into Python File and create Executable using Pyinstaller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM