通过 Jython 使用 .pyc 文件
我正在为 Python 工具构建一个 Web 界面。它是使用 J2EE (Spring) 设计的。
在此过程中,我需要调用 Python 函数,因此我使用 Jython 来实现同样的目的。
但对于某些模块,我没有 Python 源文件,只有 .pyc
文件和列出该文件方法的文档。我需要知道如何使用 jython 调用 .pyc
文件中的这些函数。
我尝试反编译 Python 文件,但由于它们已符合 Python 2.7,我无法找到反编译器来完成这项工作
I am working on building a web interface for a Python tool. It's being designed using J2EE (Spring).
In the process, I need to make calls to Python functions and hence I am using Jython for the same.
But for some modules I don't have the Python source files, I only have the .pyc
files, and a document listing the methods of that file. I need to know how I can call these functions inside the .pyc
file using jython.
I have tried to de-compile the Python files but since they have been complied with Python 2.7, I am not able find a decompiler to do the job
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“编译”的 python 代码“.pyc”文件是特定于实现的。即使 CPython(标准 Python 实现)也无法导入由不同版本的 CPython 生成的 .pyc 文件。并且不应该这样做。因此,如果 Jython 能够运行由任何 CPython 版本创建的 .pyc 文件,我会感到惊讶。
“.pyc”文件与 Java 字节码(设计为可移植的)不同。
反编译似乎是唯一的方法。我认为有一些 .pyc 反编译器可用,它们应该能够生成可由 Jython 运行的 Python 代码。
The 'compiled' python code '.pyc' files are implementation-specific. Even CPython (the standard Python implementation) is not able to import .pyc files generated by a different version of CPython. And is not supposed to. So, I would be surprised if Jython had an ability to run .pyc files created by any of CPython version.
'.pyc' files are not the same as Java bytecode (which is designed to be portable).
Decompilation seems the only way. I think there are some .pyc decompilers available, they should be able to generate Python code that could be run by Jython.
实际上,jython 确实对运行 cpython 生成的 pyc 文件提供了实验性支持:jython 2.5 内置了一个 cpython 字节码解释器。
在此处了解有关如何使用它的更多信息
http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html?highlight=pycimport#compilation
Actually, jython does have experimental support for running cpython-generated pyc files: jython 2.5 has a cpython bytecode interpreter built in.
Read more about how to use it here
http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html?highlight=pycimport#compilation