使用PyInstaller将Cython编译的模块和python代码构建为可执行二进制文件 [英] Build Cython-compiled modules and python code into executable binary using PyInstaller
问题描述
我正在尝试使用Cython
和PyInstaller
库将项目代码打包为可执行二进制文件.
我的代码目录如下:
I am trying to package my project code into a an executable binary using Cython
and PyInstaller
libraries.
My code directory looks like this:
main.py
是从program_a.py
和program_b.py
导入逻辑的主要代码.
The main.py
is the main code which imports the logic from program_a.py
and program_b.py
.
我成功地将我的program_a
和program_b
文件转换为.so文件,可以通过任何python代码导入.我是通过执行以下脚本来做到这一点的.
I am successfully able to convert my program_a
and program_b
files into .so files which can be imported by any python code. 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),
)
通过执行> python setup.py build_ext --inplace
,我将得到.so
文件,如下所示
By executing >python setup.py build_ext --inplace
I get .so
files as shown below
当我运行python main.py
时,它可以与.so
文件完美地运行.这表明我可以将它们作为模块导入.
When I run python main.py
it runs perfectly with .so
files. Which shows that I can import them as a module.
现在,我想将二进制(.so)文件和main.py
打包到单个二进制文件中.为此,我使用了pyInstaller
Now, I want to package binaries (.so) files and main.py
into single binary file. For that I used the following command provided by pyInstaller
pyinstaller "main.py" --onefile
它实际上在dist/
文件夹中提供了一个二进制文件,但是我无法导入某些模块并出现以下错误:
It actually gives a binary in dist/
folder but I cannot able to import some modules and getting the following error:
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
如何将库与pyinstaller链接或将库信息嵌入到我的二进制文件中?
How can I link libraries with the pyinstaller or embed library information into my binaries?
我发现了什么
https://riptutorial. com/cython/example/21982/bundling-a-cython-program-using-pyinstaller
但是以上所有这些链接都没有在python代码示例中使用任何外部包.我可以在没有外部模块的情况下编译代码
But all of these above links are not using any external package inside there python code examples. I am able to compile the code without external modules
推荐答案
熟悉PyInstaller程序包后,我就可以确定问题所在.最后,我按照以下步骤进行操作.
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 )
这篇关于使用PyInstaller将Cython编译的模块和python代码构建为可执行二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!