Skip to main content
 首页 » 编程设计

python之如何读取Python内存中的代码文件

2025年05月04日9webabcd

这是我实际得到的错误/回溯:

Traceback (most recent call last): 
  File "/home/apache/tactic/src/tactic/ui/panel/custom_layout_wdg.py", line 619, in process_mako 
    html = template.render(server=my.server, search=Search, sobject=sobject, sobjects=my.sobject_dicts, data=my.data, plugin=plugin, kwargs=my.kwargs) 
  File "/home/apache/tactic/src/mako/template.py", line 189, in render 
    return runtime._render(self, self.callable_, args, data) 
  File "/home/apache/tactic/src/mako/runtime.py", line 403, in _render 
    _render_context(template, callable_, context, *args, **_kwargs_for_callable(callable_, data)) 
  File "/home/apache/tactic/src/mako/runtime.py", line 434, in _render_context 
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs) 
  File "/home/apache/tactic/src/mako/runtime.py", line 457, in _exec_template 
    callable_(context, *args, **kwargs) 
  File "memory:0x7f2ea8589810", line 19, in render_body 
    ${gl.get_gantt('vfx')} 
  File "/home/apache/gantt_logic.py", line 34, in get_gantt 
    prepend.add_style("width: %spx" %(total_width/days * (start_date - process[1]["start"]).days)) 
ZeroDivisionError: integer division or modulo by zero 

我不是在问如何修复 ZeroDivisionError 错误。这是微不足道的。

我想知道的是,如何读取 Python 内存中的文件?在这种情况下 memory:0x7f2ea8589810。

请您参考如下方法:

我现在不了解你的情况,但 import linecache 是你在需要文件行时会询问的模块。

我还认为 traceback 对这一行使用了 linecache:

${gl.get_gantt('vfx')} 

所以我认为想要读取他们生成的代码的开发人员应该创建 linecache 支持。

尝试:

import linecache 
print(linecache.getlines("memory:0x7f2ea8589810")) 

或类似的东西。