Python 在 PyCharm 的控制台中运行代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21516027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running code in PyCharm's console
提问by night_bat
Are there any smooth way to run Python scripts in the PyCharm's console?
在 PyCharm 的控制台中运行 Python 脚本有什么流畅的方法吗?
My previous IDE - PyScripter - provides me with that nice little feature. As far as I know PyCharm has 2 ways of running script in console: 1) Select a bunch of code and press Ctrl+Alt+E. 2) Save the code in a file and import it from the Console.
我以前的 IDE - PyScripter - 为我提供了这个不错的小功能。据我所知,PyCharm 有两种在控制台中运行脚本的方法:1)选择一堆代码并按Ctrl+ Alt+ E。2) 将代码保存在文件中并从控制台导入。
Are the any way to do it by pressing "Run" or "Debug" buttons? I need to see the result of my script in the console and all variables available to manipulate.
有没有办法通过按“运行”或“调试”按钮来做到这一点?我需要在控制台中查看脚本的结果以及所有可操作的变量。
回答by zvisofer
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.
备注:在运行按钮旁边,您可以通过选择其显示名称来监控您运行的脚本/配置。
回答by Mr. Girgitt
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
这是 PyCharm 的网络帮助链接,其中涵盖了运行/调试配置: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.
注释中添加了一种使用调试模式和评估表达式窗口操作变量的可能方法,但我错过了一个细节:要查看交互式代码执行的结果,您必须从调试器切换到控制台输出;模式选项卡位于底部窗格的左上角。
回答by Patrick
In the Run/Debug Configuration, add -i to the interpreter options. This will stop it from closing a python session even after a successful run. I.e. you will be able to see all the variable contents
在运行/调试配置中,将 -i 添加到解释器选项中。即使在成功运行后,这也将阻止它关闭 python 会话。即您将能够看到所有变量内容
回答by rhody
The way I do it is a create my script in the editor, call it myfirst.py, eg
我这样做的方式是在编辑器中创建我的脚本,将其命名为 myfirst.py,例如
print "Hello"
a= 1234
then in the console I run:
然后在控制台中我运行:
reload (myfirst)
To check on the variables use:
要检查变量,请使用:
>>> myfirst.a
Not perfect but that's pyCharm for you. If you want a pyScripter like experience which I think is more useful you can try spyder2.
不完美,但这对你来说是 pyCharm。如果你想要类似 pyScripter 的体验,我认为它更有用,你可以尝试 spyder2。
回答by Ansgar
It may also be a good option for you to use the magic command
使用魔法命令也可能是一个不错的选择
%run scriptname.py
in the ipython console (including the %-character). Compared to Cntrl + Alt + E you also get clean information on errors in your code. Due to autocomplete it's also typed in a second and you can use this in any environment with an ipython shell. For me as a scientist who often uses python to explore data, this is a good option.
在 ipython 控制台中(包括 % 字符)。与 Cntrl + Alt + E 相比,您还可以获得有关代码中错误的清晰信息。由于自动完成,它还在一秒钟内输入,您可以在任何带有 ipython shell 的环境中使用它。对于我这个经常使用python探索数据的科学家来说,这是一个不错的选择。
回答by Tim
With your program in the editor:
在编辑器中使用您的程序:
Run - Edit Configurations
运行 - 编辑配置
Then under Execution check the box for "Run with Python console".
然后在执行下选中“使用 Python 控制台运行”框。
When you run the program from the green arrow it will run in a console. After the run all your objects are available to examine and play with.
当您从绿色箭头运行程序时,它将在控制台中运行。运行后,您可以检查和使用所有对象。


