简体   繁体   English

如何在代码更改后重新启动pydev交互式控制台?

[英]How to restart pydev interactive console after a code change?

I use Eclipse with pydev for python development. 我使用Eclipse和pydev进行python开发。 I would like to test my code using pydev's interactive python console. 我想使用pydev的交互式python控制台测试我的代码。 When I make a change in the code, interactive pydev console doesn't notice it, and i have to close the current pydev console and open a new one to reflect the changes. 当我在代码中进行更改时,交互式pydev控制台不会注意到它,我必须关闭当前的pydev控制台并打开一个新的控制台以反映更改。 Is there an easier way to get the changes to current active console? 有没有更简单的方法来获得当前活动控制台的更改? maybe a restart button? 也许是一个重启按钮?

Ok, I found a cool way to do it. 好的,我发现了一个很酷的方法。 Whenever you launch a new Pydev console; 每当你启动一个新的Pydev控制台; choose the option of Console for currently active editor . 为当前活动的编辑器选择Console选项。

Within the python prompt in the interactive console type the following 在交互式控制台的python提示符下键入以下内容

execfile('<full_path_to_your_python_script>')

Now you can experiment with the code in your script. 现在,您可以在脚本中试验代码。

If you change something within the file (such as a method or class definition), then again within the same Pydev console execute the execfile statement. 如果您更改了文件中的某些内容(例如方法或类定义),则再次在同一个 Pydev控制台中执行execfile语句。 This will re-load the currently active editor file and you will now have access the modified code. 这将重新加载当前活动的编辑器文件,您现在可以访问修改后的代码。

Note: I guess this behavior is because, the importing activity in Python are idempotent - ie a module can imported only once. 注意:我猜这种行为是因为,Python中的导入活动是幂等的 - 即模块只能导入一次。 After it has been imported, any subsequent imports for the module will refer and return the previously imported module instance. 导入后,模块的任何后续导入都将引用并返回先前导入的模块实例。 The only way to get the latest definition of the module then is to use a reload('module_name') method. 获取模块的最新定义的唯一方法是使用reload('module_name')方法。 I guess this is what execfile does - it reloads the python module corresponding to the currently active editor file. 我想这是execfile作用 - 它重新加载对应于当前活动编辑器文件的python模块。

在编码窗口中进行ctrl + alt + 输入 (控制台是否打开),您将自动加载当前代码以供使用。

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

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