16

在 PyCharm 的控制台中运行 Python 脚本有什么流畅的方法吗?

我以前的 IDE - PyScripter - 为我提供了这个不错的小功能。据我所知,PyCharm 有两种在控制台中运行脚本的方式:1)选择一堆代码并按Ctrl+ Alt+ E。2) 将代码保存在文件中并从控制台导入。

有没有办法通过按“运行”或“调试”按钮来做到这一点?我需要在控制台中查看脚本的结果以及所有可操作的变量。

4

6 回答 6

28

在运行/调试配置中,将 -i 添加到解释器选项中。即使成功运行,这也会阻止它关闭 python 会话。即你将能够看到所有的变量内容

于 2014-03-13T18:13:14.233 回答
3

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.

于 2014-02-02T20:53:00.897 回答
3

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.

于 2014-02-02T20:54:00.097 回答
1

我这样做的方式是在编辑器中创建我的脚本,称之为 myfirst.py,例如

print "Hello"
a= 1234

然后在控制台中我运行:

reload (myfirst)

要检查变量,请使用:

>>> myfirst.a

不完美,但这对你来说是 pyCharm。如果你想要一个我认为更有用的类似 pyScripter 的体验,你可以试试 spyder2。

于 2015-04-28T23:19:49.210 回答
1

使用魔法命令也可能是一个不错的选择

%run scriptname.py

在 ipython 控制台中(包括 % 字符)。与 Cntrl + Alt + E 相比,您还可以获得有关代码错误的清晰信息。由于自动完成,它也可以在一秒钟内输入,您可以在任何带有 ipython shell 的环境中使用它。对于我这个经常使用 python 来探索数据的科学家来说,这是一个不错的选择。

于 2018-03-02T08:43:12.250 回答
0

在编辑器中使用您的程序:

运行 - 编辑配置

然后在执行下选中“使用 Python 控制台运行”框。

当您从绿色箭头运行程序时,它将在控制台中运行。运行后,您的所有对象都可以检查和使用。

在此处输入图像描述

于 2019-04-22T11:11:55.627 回答