简体   繁体   English

如何使 PyCharm Python 控制台与代码一起工作?

[英]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:您有几种方法可以做到这一点:

  1. Execute Selection in Console - This will execute any highlighted code in the console: Execute Selection in Console - 这将Execute Selection in Console执行任何突出显示的代码:

    Pycharm 窗口的图像,在突出显示的代码行上显示右键单击上下文菜单。突出显示了“在控制台中执行选择”选项 Note that if no code is highlighted, this option will become Execute Line in Console .请注意,如果没有突出显示代码,则此选项将Execute Line in Console变为Execute Line in Console

  2. One option below that is the Run File in Console .下面的一个选项是Run File in ConsoleRun File in Console This will, obviously, run the whole script in the console:显然,这将在控制台中运行整个脚本:

    Pycharm 窗口的图像,这次显示的是右键单击上下文菜单,没有突出显示任何内容。现在,突出显示了“在控制台中执行文件”选项

    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... :打开Edit Configurations...

      Pycharm 窗口的图像,突出显示单击右上角运行符号旁边的下拉菜单后显示的选项“编辑配置”

    • Then, under Execution , mark the Run with Python console option:然后,在Execution下,标记Run with Python console选项:

      显示“编辑配置”窗口的图像,其中突出显示了“使用 Python 控制台运行”选项

    • Now you can run the file regularly, by selecting Run File or using the green triangle.现在您可以通过选择Run File或使用绿色三角形定期Run File

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.

相关问题 如何在Pycharm“ Python控制台”中运行代码? - How to run code in Pycharm “Python Console”? pycharm代码自动完成仅在python控制台中工作,但在python文件上不起作用 - pycharm code autocomplete work only in python console but not on python file pycharm代码自动完成不适用于python控制台与ipython - pycharm code autocomplete does not work for python console with ipython 如何使全局变量与 Python 中的 Pycharm 一起使用? - How to make global variables work with Pycharm in Python? 在 Pycharm 中运行代码后,如何在 Python 控制台中显示变量? - How to show variables in Python Console after running code in Pycharm? 如何在 PyCharm 中重新初始化 Python 控制台? - How to reinitialize the Python console in PyCharm? 如何制定工作命令(PyCharm 2020.3.2 Python 3.9) - How do I make a work command (PyCharm 2020.3.2 Python 3.9) PyCharm:让“在控制台中执行代码”停止询问我是哪个控制台 - PyCharm: Make 'Execute code in console' stop asking me which console 如何以交互方式将代码的输出输入到另一个代码中,并从Pycharm交互式python控制台运行它们? - How to interactively input the output of a code into another code and run them from Pycharm interactive python console? 如何在pycharm的python控制台中重新加载更新的功能 - how to reload updated function in python console in pycharm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM