在 PyCharm 的控制台中运行 Python 脚本有什么流畅的方法吗?
我以前的 IDE - PyScripter - 为我提供了这个不错的小功能。据我所知,PyCharm 有两种在控制台中运行脚本的方式:1)选择一堆代码并按Ctrl+ Alt+ E。2) 将代码保存在文件中并从控制台导入。
有没有办法通过按“运行”或“调试”按钮来做到这一点?我需要在控制台中查看脚本的结果以及所有可操作的变量。
在 PyCharm 的控制台中运行 Python 脚本有什么流畅的方法吗?
我以前的 IDE - PyScripter - 为我提供了这个不错的小功能。据我所知,PyCharm 有两种在控制台中运行脚本的方式:1)选择一堆代码并按Ctrl+ Alt+ E。2) 将代码保存在文件中并从控制台导入。
有没有办法通过按“运行”或“调试”按钮来做到这一点?我需要在控制台中查看脚本的结果以及所有可操作的变量。
在运行/调试配置中,将 -i 添加到解释器选项中。即使成功运行,这也会阻止它关闭 python 会话。即你将能够看到所有的变量内容
Run -> Edit configuration -> select the script you want to run and give it a display name -> OK
Now you can run it with the green "Run" button. The green bug button (next to the run button) will run it in debug mode.
Remark: next to the run button you can monitor the script/configuration you run by selecting it's display name.
If you create run-time configuration then after pressing the run button (green "play" icon) you will get the desired result: your code will give you output in a console which opens atomatically at the bottom.
You can create many different configuraions for run and debug within a single project.
Here is a link from PyCharm's web help which covers the run/debug configurations: http://www.jetbrains.com/pycharm/webhelp/run-debug-configuration-python.html
A possible way of manipulating the variables using debug mode and evaluate expression window is added in comment but I missed one detail: to see the result of your interactive code execution you have to switch from Debugger to Console output; mode tabs are on the top-left side of the bottom pane.
我这样做的方式是在编辑器中创建我的脚本,称之为 myfirst.py,例如
print "Hello"
a= 1234
然后在控制台中我运行:
reload (myfirst)
要检查变量,请使用:
>>> myfirst.a
不完美,但这对你来说是 pyCharm。如果你想要一个我认为更有用的类似 pyScripter 的体验,你可以试试 spyder2。
使用魔法命令也可能是一个不错的选择
%run scriptname.py
在 ipython 控制台中(包括 % 字符)。与 Cntrl + Alt + E 相比,您还可以获得有关代码错误的清晰信息。由于自动完成,它也可以在一秒钟内输入,您可以在任何带有 ipython shell 的环境中使用它。对于我这个经常使用 python 来探索数据的科学家来说,这是一个不错的选择。