简体   繁体   English

使用已编译的python文件检查模块

[英]inspect module with compiled python files

I have two files caller.py and callee.py 我有两个文件caller.py和callee.py

caller.py 呼叫者

## 
##     
##     
##     
##   
##   
##
##
##
from callee import fn
def caller():
  fn()

caller()

callee.py 被叫人

import inspect
def fn():
  print inspect.stack()

Now I compiled both caller.py and callee.py to get the corresponding pyc files and moved them to a new directory compiled . 现在,我同时编译了caller.pycallee.py以获得相应的pyc文件,并将它们移至已compiled的新目录中。 This is the directory structure. 这是目录结构。

├── __init__.py
├── callee.py
├── caller.py
└── compiled
    ├── __init__.pyc
    ├── callee.pyc
    └── caller.pyc

I modified caller.py to remove all the comments, resulting in: caller.py 我修改了caller.py以删除所有注释,结果是:caller.py

from callee import fn
def caller():
  fn()

caller()

When I execute python compiled/caller.pyc This is the error thrown 当我执行python compiled/caller.pyc这是抛出的错误

Traceback (most recent call last): File "caller.py", line 73, in 追溯(最近一次通话):文件“ caller.py”,行73,在

File "caller.py", line 71, in caller 呼叫者中的文件“ caller.py”,第71行

File "callee.py", line 4, in fn 在fn中,文件“ callee.py”,第4行

File "/usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 1062, in stack return getouterframes(sys._getframe(1), context) File "/usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 1040, in getouterframes framelist.append((frame,) + getframeinfo(frame, context)) File "/usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 1015, in getframeinfo lines, lnum = findsource(frame) File "/usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 579, in findsource if pat.match(lines[lnum]): break IndexError: list index out of range 文件“ /usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py”,行1062,在堆栈中返回getouterframes(sys._getframe( 1),上下文)文件“ /usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py”,行1040,位于getouterframes框架列表中。 append((frame,)+ getframeinfo(frame,context))文件“ /usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py “,第1015行,在getframeinfo行中,lnum = findsource(frame)文件” /usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect。 py“,第579行,如果pat.match(lines [lnum]):breakIndexError:列表索引超出范围,则在findsource中

Looks like inspect is trying to read from the source file .py even though it is in a different directory. 看起来好像inspect正在尝试从源文件.py读取,即使它在另一个目录中也是如此。 This error does not seem to occur when I navigate to some other directory. 当我导航到其他目录时,似乎没有发生此错误。

What is the explanation for this? 这有什么解释? Does inspect.stack() look for the filename's (caller.pyc) py equivalent in the current working directory? inspect.stack()是否在当前工作目录中查找等效的文件名(caller.pyc) py

Python looks for suitable modules based on its PATH , and the first place it looks for them is in the folder you are when you launch the program. Python根据其PATH寻找合适的模块,并且寻找它们的第一位是启动程序时所在的文件夹。

If you want the interpreter to ignore the original source files, move to the directory where the .pyc files are and launch the script with python caller.pyc 如果您希望解释器忽略原始源文件,请移至.pyc文件所在的目录,然后使用python caller.pyc启动脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM