以编程方式添加 python 解释器 [英] adding python interpreters programmatically

查看:55
本文介绍了以编程方式添加 python 解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有使用 Pydev 插件界面添加和更改解释器的简单方法?我正在运行 pydev 1.6.1,我希望能够根据我环境中的可用解释器列表添加和使用给定的解释器.

Is there a simple way to add and change interpreters using the Pydev plugin interface? I'm running pydev 1.6.1 and I'd like to be able to add and use a given interpreter based on a list of available interpreters in my environment.

现在我可以看到 PythonInterpreterManager 有一个 createInterpreterInfo 调用,但这似乎没有任何作用.查看 pydev 的源代码,似乎我必须实际使用首选项页面来跟踪所有这些页面.

Right now I can see the PythonInterpreterManager has a createInterpreterInfo call, but that doesn't seem to do anything. Looking at the source for pydev, it seems like I have to actually work with the preference pages to keep track of all of them.

我可以调用一组更简单的函数来添加这些函数吗?

Is there a simpler set of functions I can call to add these?

PythonInterpreterManager manager = (PythonInterpreterManager)PydevPlugin.getPythonInterpreterManager(true);
IInterpreterInfo info = manager.createInterpreterInfo(execPath, new NullProgressMonitor());
        manager.addInterpreterInfo(info);

我已经可以执行上述操作,但这只会缓存它,但不会在首选项中将其显示为有效的解释器选项.

I can already do the above, but that only caches it, but doesn't display it as a valid interpreter option in the preferences.

我什至尝试通过反射来添加这些,但运气不佳.我可以像 Add 按钮那样在编辑器上调用 addNewInput ,但是它说它不知道该解释器.我已经尝试创建一个弹出首选项页面,并像 getNewInput() 那样通过反射将值添加到各个成员中,但仍然没有在首选项页面中看到更多选项.我不确定这是因为我遗漏了什么,还是我制作的弹出式偏好页面与使用窗口"->偏好设置"下拉菜单弹出的页面完全无关.

I've even tried doing reflection to add these without much luck. I can call addNewInput on the editor as the Add button does, but then it says it doesn't have any knowledge of that interpreter. I've tried creating a popup preferences page and adding the values to the various members via reflection as getNewInput() would, but still don't see any more options in the preference page. I'm not sure if this is because I'm missing something or the popup preference page I make is totally unrelated to the page that pops up using the Window->"Preferences" pulldown.

推荐答案

API 是这样的:

    IInterpreterManager iMan = PydevPlugin.getPythonInterpreterManager(true);
    IInterpreterInfo interpreterInfo = iMan.createInterpreterInfo("c:/python/python.exe", monitor, false);
    iMan.setInfos(new IInterpreterInfo[]{interpreterInfo}, null, null);

请注意,如果您在那里有manager.addInterpreterInfo",您可能有一个旧版本的 PyDev...(在那个调用中,您设置了所有可用的解释器,因此,如果您想保留一些配置,您应该查询它并将它们添加回来).

Note that if you have 'manager.addInterpreterInfo' in there, you probably have an old version of PyDev... (and at that call you set all the interpreters available, so, if you want to keep some configuration, you should query it and add them back).

您可以使用:org.python.pydev.editor.codecompletion.revisited.javaintegration.AbstractWorkbenchTestCase.createPythonInterpreterManager(NullProgressMonitor) 作为参考.

You can use: org.python.pydev.editor.codecompletion.revisited.javaintegration.AbstractWorkbenchTestCase.createPythonInterpreterManager(NullProgressMonitor) as a reference.

这篇关于以编程方式添加 python 解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆