Python,Pyinstaller打包含taichi模块的程序

Python版本3.9、taichi版本1.7.1,pyinstaller版本6.9.0

问题描述:

正常Pyinstaller打包后报错

[Taichi] version 1.7.1, llvm 15.0.1, commit 0f143b2f, win, python 3.9.19
[Taichi] Starting on arch=x64
Traceback (most recent call last):
  File "taichi\lang\_wrap_inspect.py", line 102, in _Python_IPython_findsource
  File "inspect.py", line 835, in findsource
OSError: could not get source code

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "taichi\lang\_wrap_inspect.py", line 149, in _custom_findsource
  File "taichi\lang\_wrap_inspect.py", line 136, in _Python_IPython_findsource
OSError: Cannot find source code for Object: <function my_HSpace at 0x0000021D7DD790D0>, it's likely you are not running Taichi from command line or IPython.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "taichi\lang\_wrap_inspect.py", line 152, in _custom_findsource
  File "taichi\lang\_wrap_inspect.py", line 144, in _REPL_findsource
  File "dill\source.py", line 176, in findsource
OSError: could not extract source code

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "taichi\lang\_wrap_inspect.py", line 57, in _blender_findsource
ModuleNotFoundError: No module named 'bpy'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "taichi\lang\_wrap_inspect.py", line 155, in _custom_findsource
  File "taichi\lang\_wrap_inspect.py", line 59, in _blender_findsource
ImportError: Not in Blender environment!

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 30, in <module>
    my_hough.my_HSpace(img_kernel, my_hough_space, 2, 0.05, 150, 550, 300, 400, 270, 280)
  File "taichi\lang\kernel_impl.py", line 1113, in wrapped
  File "taichi\lang\kernel_impl.py", line 1043, in __call__
  File "taichi\lang\kernel_impl.py", line 1011, in ensure_compiled
  File "taichi\lang\kernel_impl.py", line 645, in materialize
  File "taichi\lang\kernel_impl.py", line 133, in _get_tree_and_ctx
  File "taichi\lang\_wrap_inspect.py", line 176, in getsourcelines
  File "inspect.py", line 1006, in getsourcelines
  File "taichi\lang\_wrap_inspect.py", line 157, in _custom_findsource
OSError: Cannot find source code for Object: <function my_HSpace at 0x0000021D7DD790D0>, this is possibly because of you are running Taichi in an environment that Taichi's own inspect module cannot find the source. Please report an issue to help us fix: https://github.com/taichi-dev/taichi/issues
[18132] Failed to execute script 'main' due to unhandled exception!

解决方案:

截止20240726官方给到的方案是正常打包是无解的,各位果断放弃!如果有解决的大佬,请提醒我改正。

参考地址:https://github.com/taichi-dev/taichi/issues/4936

调整taichi的使用方式,可以正常打包,说明如下:

I manged to get a work around demo working.
It seems the reason is the taichi using inspect to read the source code,
while the pyinstaller pack everyting in to .pyc file,
making the inspect unable to read the source code.
to debug, a simple example to inspect.getsource, whenever inspect.getsource works, the taichi can compile and run the kernel.

for ref
python - Problem with inspect using PyInstaller; can get source of class but not function - Stack Overflow

To make it work, I have to put all the kernel code into a seprate file and use import the code dynamically. use importlib rather than import, and also add the path to system enable the exe find the local .py file

## test_inspect.py 
import inspect

import sys
import os

sys.path.append(os.getcwd())

import importlib 

# load the mode dyanically from sys.path
code=importlib.import_module('cc')

import taichi as ti 
ti.init(arch=ti.gpu)

if __name__=="__main__":

   #check the system include the cwd
    print(sys.path)
   
   #try to get the source for code.hello
    ff=inspect.getsourcefile(code.hello)
    print('modelfil',ff)
    src=inspect.getsource(code.hello)
    print(src)

    #test the kernel
    code.hello()
    
#cc.py
import taichi as ti 
#ti.init(arch=ti.gpu)

@ti.kernel
def hello():
    for i in range(100):
        print(i,"hello")

执行,pyinstaller -F -c test_inpsect.py,打包后可正常运行

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值