首页 > 编程语言 >Python,Pyinstaller打包含taichi模块的程序

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

时间:2024-07-26 14:26:30浏览次数:17  
标签:lang py Pyinstaller taichi Python inspect File line

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,打包后可正常运行

标签:lang,py,Pyinstaller,taichi,Python,inspect,File,line
From: https://blog.csdn.net/qq_61523551/article/details/140714340

相关文章

  • Python,运行Yolo项目,报错AttributeError: ‘ImageDraw‘ object has no attribute ‘te
    Python3.9问题描述:其他电脑已经运行成功的Python,YOLO代码到我电脑上运行报错Traceback(mostrecentcalllast): File"C:\Users\Administrator\Desktop\20240725\识别项目\predict.py",line122,in<module>  frame=np.array(yolo.detect_image(frame)) Fil......
  • Python从零开始制做文字游戏(荒岛求生)
    文章目录前言开发游戏《荒岛求生》游戏大纲背景内容通关条件游戏过程探索荒岛购买物资休息总结代码开发定义变量当前代码引入背景故事当前代码循环问题解决:函数当前代码制作延时当前代码制作a函数(探索荒岛阶段)展示数......
  • 使用 Python 进行数据分析:入门指南
    使用Python进行数据分析:入门指南1.简介本指南将介绍如何使用Python进行数据分析,涵盖从数据加载到可视化分析的各个方面。2.必要的库NumPy:用于数值计算和数组操作。Pandas:用于数据处理和分析,提供DataFrame结构。Matplotlib:用于数据可视化,创建各种图表。Seab......
  • IT实战课堂计算机毕业设计源码精品基于Python的高校教育教材采购出入库进销存储信息管
    项目功能简介:《[含文档+PPT+源码等]精品基于Python的高校教育教材信息管理系统设计与实现》该项目含有源码、文档、PPT、配套开发软件、软件安装教程、项目发布教程、包运行成功以及课程答疑与微信售后交流群、送查重系统不限次数免费查重等福利!软件开发环境及开发工具:开......
  • 为什么我的 Python 脚本失败并出现 TypeError?
    我正在编写一个Python脚本,该脚本应该计算数字列表的总和。但是,当我运行代码时遇到TypeError这是一个最小的例子:numbers=[1,2,3,'4']total=sum(numbers)print(total)Theerrormessageis:TypeError:unsupportedoperandtype(s)for+:'int'and'str......
  • 如何通过socks代理传递所有Python的流量?
    有如何通过http代理传递所有Python的流量?但是,它不处理sock代理。我想使用sock代理,我们可以通过ssh隧道轻松获得它。ssh-D5005user@server你可以使用socks库,让你的Python代码通过SOCKS代理传递所有流量。这个库可以让你在套接字级别上指定代......
  • 如何在streamlit python中流式传输由LLM生成的输出
    代码:fromlangchain_community.vectorstoresimportFAISSfromlangchain_community.embeddingsimportHuggingFaceEmbeddingsfromlangchainimportPromptTemplatefromlangchain_community.llmsimportLlamaCppfromlangchain.chainsimportRetrievalQAimports......
  • python mysql操作
    pipinstallmysql-connector-pythonimportmysql.connector#配置数据库连接参数config={'user':'your_username','password':'your_password','host':'your_host','database'......
  • Python 中的面向对象编程
    一.介绍在本文中,我们将使用Python中的类和对象来探索基本的OOP概念。面向对象编程(OOP)是一种强大的方法,可帮助开发人员组织代码,使其易于理解、重用和维护。Python是一种灵活的语言,可以很好地支持OOP概念。1.类和对象类是创建对象的蓝图。它定义了该类的对象将......
  • 学习资源系列之《Python深度学习基于PyTorch》
     前言近期应部分读者朋友的强烈邀请,希望推荐一本python深度学习实操的书籍。呐,今天为大家推荐小编偶然发现的这一本珍藏好书:《Python深度学习基于PyTorch》,文末附电子版获取方式《Python深度学习基于PyTorch》BriefIntroduction前言面对众多的深......