如何使 PyCharm Python 控制台与代码一起工作? [英] How to make PyCharm Python console work with code?

查看:91
本文介绍了如何使 PyCharm Python 控制台与代码一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 PyCharm 的新手(来自 IDLE)并且仍在尝试了解它是如何工作的.我在 MacOS Mojave 上运行 PyCharm Professional 2019.3.1.

我要问的是,当我在 PyCharm 中运行代码时,变量和数据不会存储在 Python 控制台中 - 它保持完全空白,我必须在控制台中单独写入.

例如,在空闲状态:

当我在我的程序中编写x = 2并执行它时,我可以在IDLE的控制台中浏览和操作x的值(通过输入> x = 3, > x = 0 等),但是当我在 PyCharm 的控制台中执行相同操作时,它说 x 未定义.

解决方案

您可能只是通过使用绿色箭头或任何等效的方式来运行代码.问题是这将脚本作为一个单独的进程运行,一旦它完成,它的环境中就没有任何东西了.


如果您想像在 IDLE 中一样运行,您可以在其中执行脚本,然后可以访问和修改环境(定义的变量和函数)-您想要 注意如果没有代码高亮,这个选项会变成Execute Line in Console.

  • 下面的一个选项是在控制台中运行文件.显然,这将在控制台中运行整个脚本:

    注意现在如何在右侧的变量视图中提供脚本中定义的所有变量.

  • 最后一种方法是在运行配置中启用它:

    • 打开Edit Configurations...:

    • 然后,在Execution下,标记Run with Python console选项:

    • 现在您可以通过选择Run File 或使用绿色三角形定期运行文件.

  • 注意:使用方法(2)自动启用方法(3)中描述的配置.所以基本上在控制台中运行一次文件后(如方法 (2) 中所述),您可以返回定期运行文件,并且所有未来的运行也将在控制台中(直到您取消选中运行配置中的复选框).

    I am new to PyCharm (coming from IDLE) and still trying to understand how it works. I'm running PyCharm Professional 2019.3.1 on MacOS Mojave.

    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.

    For instance, in 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.

    解决方案

    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. You have a few ways to do that:

    1. Execute Selection in Console - This will execute any highlighted code in the console:

      Note that if no code is highlighted, this option will become Execute Line in Console.

    2. One option below that is the 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.

    3. The last way is to enable this in the Run configurations:

      • Open the Edit Configurations...:

      • Then, under Execution, mark the Run with Python console option:

      • Now you can run the file regularly, by selecting Run File or using the green triangle.

    Note: using method (2) will automatically enable the configuration described in method (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).

    这篇关于如何使 PyCharm Python 控制台与代码一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆