简体   繁体   English

如何在脚本中使用 Python REPL

[英]How to use a Python REPL in a script

I am learning Python to use QT with Python, not only C++, and am curious if I can embed a Python interpreter in my application as a REPL?我正在学习 Python 以将 QT 与 Python 一起使用,而不仅仅是 C++,并且很好奇我是否可以在我的应用程序中嵌入一个 Python 解释器作为 REPL?

I want to allow users to script either loading a file and that file act as a plugin, or by evaluating code entered in a text box or something similar to embedding the interpreter in C or C++ and script the application using Python.我希望允许用户编写脚本,或者加载文件并将该文件作为插件,或者通过评估在文本框中输入的代码或类似于在 C 或 C++ 中嵌入解释器并使用 Python 编写应用程序脚本。

Can this be done if the application is itself written in Python and made into a standalone binary using py2exe or something similar, something like Anders did with the C# REPL or Miguel with Mono.如果应用程序本身是用 Python 编写的,并使用 py2exe 或类似的东西制作成一个独立的二进制文件,就像 Anders 用 C# REPL 或 Miguel 用 Mono 所做的那样,可以这样做吗?

Well, this is all certainly possible, but it is not beginner stuff.嗯,这当然是可能的,但这不是初学者的东西。

Python offers a read-eval loop as a module, but you'd still have so create a console in QT where you can type in input and display results. Python 提供了一个read-eval 循环作为模块,但您仍然需要在 QT 中创建一个控制台,您可以在其中输入输入并显示结果。

The same goes for a plugin system.插件系统也是如此。 It's very easy to import a script as a plugin and the plugin just has to import your application to access its state.将脚本作为插件导入非常容易,插件只需导入您的应用程序即可访问其状态。 But that's hardly a real plugin system, you'd want to create a proper API so the plugins don't break whenever something in the app changes.但这并不是一个真正的插件系统,您需要创建一个合适的 API,以便在应用程序中的某些内容发生变化时插件不会中断。

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

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