[英]How to make PyCharm Python console work with code?
I am new to PyCharm (coming from IDLE) and still trying to understand how it works.我是 PyCharm 的新手(来自 IDLE)并且仍在尝试了解它是如何工作的。 I'm running PyCharm Professional 2019.3.1 on MacOS Mojave.我在 MacOS Mojave 上运行 PyCharm Professional 2019.3.1。
What I'm asking is that when I run a code in PyCharm, the variables and data aren't stored in the Python console - it remains completely blank and I have to separately write in the console.我要问的是,当我在 PyCharm 中运行代码时,变量和数据没有存储在 Python 控制台中——它保持完全空白,我必须在控制台中单独写入。
For instance, in IDLE:例如,在 IDLE 中:
When I write x = 2
in my program and execute it, I can browse and manipulate the value of x in IDLE's console (by entering > x = 3
, > x = 0
etc.), but when I do the same in PyCharm's console it says that x is undefined.当我在程序中写入x = 2
并执行它时,我可以在 IDLE 的控制台中浏览和操作 x 的值(通过输入> x = 3
、 > x = 0
等),但是当我在 PyCharm 的控制台中执行相同操作时它说 x 是未定义的。
You are probably simply running the code by using the green arrow or any equivalent way of doing so.您可能只是使用绿色箭头或任何等效的方式运行代码。 The problem is that this runs the script as a separate process and once it finishes, nothing remains from its environment.问题是这将脚本作为一个单独的进程运行,一旦它完成,它的环境中就没有任何东西了。
If you want to run like in IDLE, where you execute the script and then can access and modify the environment (variables and functions defined) - you want to execute in console .如果您想像在 IDLE 中一样运行,您可以在其中执行脚本,然后可以访问和修改环境(定义的变量和函数) - 您想在 console 中执行。 You have a few ways to do that:您有几种方法可以做到这一点:
Execute Selection in Console
- This will execute any highlighted code in the console: Execute Selection in Console
- 这将Execute Selection in Console
执行任何突出显示的代码:
Note that if no code is highlighted, this option will become Execute Line in Console
.请注意,如果没有突出显示代码,则此选项将Execute Line in Console
变为Execute Line in Console
。
One option below that is the Run File in Console
.下面的一个选项是Run File in Console
的Run File in Console
。 This will, obviously, run the whole script in the console:显然,这将在控制台中运行整个脚本:
Notice how now you have all the variables defined in the script available in the variables view on the right side.请注意,您现在如何在右侧的变量视图中提供脚本中定义的所有变量。
The last way is to enable this in the Run configurations:最后一种方法是在运行配置中启用它:
Note: using method (2)
will automatically enable the configuration described in method (3)
.注意:使用方法(2)
将自动启用方法(3)
描述的配置。 So basically after running the file once in the console (as described in method (2)
), you can go back to running the file regularly and all future runs will be in the console as well (until you un-check the box in the run configuration).因此,基本上在控制台中运行一次文件后(如方法(2)
),您可以返回定期运行文件,并且所有未来的运行也将在控制台中(直到您取消选中运行配置)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.