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

后端 未结 2 1928
有刺的猬
有刺的猬 2021-01-17 03:04

I am trying to package my project code into a an executable binary using Cython and PyInstaller libraries. My code directory looks like this:

相关标签:
2条回答
  • 2021-01-17 03:28

    After getting familiar with PyInstaller package I am able to figure out the issue. 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 )
    
    0 讨论(0)
  • 2021-01-17 03:35

    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.

    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). For example, assume that program_a.py uses opencv library (cv2) and program_b.py uses matplotlib library. Now in your main.py file you need to import cv2 and matplotlib as well. Basically, whatever you import in program_a.py and program_b.py, you have to import that in main.py as well. This tells pyinstaller that the program needed these libraries and it includes those libraries in the exe file.

    0 讨论(0)
提交回复
热议问题