PyCharm和Python控制台 [英] PyCharm and Python Console
问题描述
我想要配置PyCharm:我需要在主编辑窗口中写我的脚本,然后我想检查在Python控制台窗口是否结果(变量,列表)工作正常。
I am trying to configure PyCharm: I need to write my script in the main editing window and then I want to check in the Python console window whether the results (variables, lists) work as expected.
但是,当我在运行脚本后开始使用Python控制台时,控制台没有从正在运行的进程收集任何数据。
Nonetheless, as I start using the Python console after running the script, the console has not collected any data from the running process.
推荐答案
Python控制台是一个单独的环境,您可以在运行代码时按运行...。
The "Python Console" is a separate environment from the one you run code in when you press "Run...".
首先:使用PyCharm提供的优秀的调试工具来遍历你的代码,看看它们是否按原样工作。
First of all: Use the excellent debugging facilities provided by PyCharm to step through your code and see whether things work as they should in situ.
如果由于某种原因不能为您做的:
If that for some reason doesn't do it for you:
您可以从 import< yourfile>
,脚本将运行,但你的变量只能作为< yourfile>。< var ;
。当然,如果__name__ =='__main __',那么下的任何内容都不会被执行。
You can from <yourfile> import *
in the console, if running pure python. That will get you your variables. If you do import <yourfile>
, the script will be run, but your variables will only be available as <yourfile>.<var>
. Of course, anything you have under if __name__ == '__main__'
won't be executed.
不会,我会引荐您 http://stackoverflow.com/a/437857/3745323 :
with open("yourfile.py") as f:
code = compile(f.read(), "yourfile.py", 'exec')
exec(code, global_vars, local_vars)
定义为作为单独文件中的函数并导入它。
You can definen that as a function in a separate file and import it. Alternatively, just type it into the interpreter.
如果您安装了IPython,最近的PyCharm版本默认使用它在控制台中。在这种情况下,您可以使用%run
magic来运行您的脚本,就像在解释器中逐行键入一样。
If you have IPython installed, recent PyCharm editions default to using it in the console. In this case you can use the %run
magic to run your script as if it was typed in line-by-line into the interpreter.
In[2]: %run yourfile
这篇关于PyCharm和Python控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!