Python 从 pycharm 控制台运行模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16874046/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:54:29  来源:igfitidea点击:

Running a module from the pycharm console

pythonconsolepycharm

提问by user2443457

I'm new to python and pycharm and I'd like to run a module from the pycharm console in the same way as you can from IDLE, if it's possible.

我是 python 和 pycharm 的新手,如果可能的话,我想以与从 IDLE 相同的方式从 pycharm 控制台运行模块。

The idea is to create simple functions and test them "live" using the console.

这个想法是创建简单的函数并使用控制台“实时”测试它们。

...how do you do that in pycharm?

...你如何在 pycharm 中做到这一点?

回答by alecxe

Running python scripts using pycharm is pretty straightforward, quote from docs:

使用 pycharm 运行 python 脚本非常简单,引用自docs

To run a script with a temporary run/debug configuration Open the desired script in the editor, or select it in the Project tool window. Choose Run on the context menu, or press Ctrl+Shift+F10. So doing, a temporary run/debug configuration is created on-the-fly.

使用临时运行/调试配置运行脚本 在编辑器中打开所需的脚本,或在项目工具窗口中选择它。在上下文菜单上选择运行,或按 Ctrl+Shift+F10。这样做时,会即时创建临时运行/调试配置。

Besides there is a "Python Console" available in pycharm: see documentation.

此外,pycharm 中还有一个“Python 控制台”可用:请参阅文档

UPD: Here's an example.

UPD:这是一个例子。

Imagine you have a python module called test_module.py:

假设您有一个名为的 Python 模块test_module.py

def a(*args, **kwargs):
    print "I'm function a"

def b(*args, **kwargs):
    print "I'm function b"

Then, in pycharm's "Python Console" you can do this:

然后,在 pycharm 的“Python 控制台”中,您可以执行以下操作:

>>> from test_module import *
>>> a()
I'm function a
>>> b()
I'm function b


If you need to execute a part of an existing code, you can use the Execute Selection in Consolefeature: select the code snippet -> right click -> "Execute Selection in Console".

如果您需要执行现有代码的一部分,您可以使用在控制台中执行选择功能:选择代码片段-> 右键单击​​->“在控制台中执行选择”。

回答by Piotr Dobrogost

What you're looking for is the feature called Execute Selection in Consolewhich is described in section Loading Code from Editor Into Consoleof PyCharm's online help.

您正在寻找的是名为Execute Selection in Console的功能,该功能PyCharm 在线帮助的将代码从编辑器加载到控制台部分中进行了描述。

回答by user3507584

Select the script lines that you want to execute and press Shift+Alt+E

选择要执行的脚本行并按 Shift+Alt+E

回答by Rampkins

For anyone still having this problem: Go to the Run/Debug menu, choose Edit Configuration, check the box 'Show command line' this will enable you to enter parameters in the console at the >>> prompt and test your function.

对于仍然有此问题的任何人:转到“运行/调试”菜单,选择“编辑配置”,选中“显示命令行”框,这将使您能够在 >>> 提示符下在控制台中输入参数并测试您的功能。

Edit: To make this change apply to all your .py files (as this check box only applies to the current file you're working on) go to: Edit configuration, in the pop up you will see a menu tree on the left, select Defaults, then Python, then check the 'Show command line' box, this will make it the default setting whenever you open a .py file, (this feature should really be on by default!)

编辑:要将此更改应用于所有 .py 文件(因为此复选框仅适用于您正在处理的当前文件)转到:编辑配置,在弹出窗口中,您将在左侧看到一个菜单树,选择默认值,然后选择 Python,然后选中“显示命令行”框,这将使其成为每次打开 .py 文件时的默认设置(默认情况下,此功能应该真正启用!)

回答by StackG

Run File In Console

在控制台中运行文件

Right Click --> Run File In Console

右键单击 --> 在控制台中运行文件

Done!

完毕!

enter image description here

在此处输入图片说明

回答by Nadav B

In pycharm do:

在 pycharm 中:

Run>Edit Configuration>Show command line afterwards

回答by flow2k

Looks like in version 2018.3, this option is now Run with Python consolein Run/Debug Configurations: enter image description here

看起来在 2018.3 版本中,此选项现在Run with Python console位于Run/Debug Configurations在此处输入图片说明

回答by lmiguelvargasf

You can run the Find Actionshortcut (Ctrl+Shift+Aor ?+?+Aon mac), then type run file, and choose the option Run file in Console.

您可以运行查找操作快捷键(Ctrl+ Shift+A?+ ?+A在Mac),然后键入运行文件,并选择选项Run file in Console

enter image description here

在此处输入图片说明

回答by Martin Wei?

Assuming your code is in file MySimpleCode.pyyou can simply say

假设您的代码在文件中,MySimpleCode.py您可以简单地说

run MySimpleCode

in the PyCharm console. This assumes that you have set your working directory properly; e.g. if MySimpleCode.pyis located in d:\workon a Windows system you must execute

在 PyCharm 控制台中。这假设您已正确设置工作目录;例如,如果MySimpleCode.py位于d:\workWindows 系统上,您必须执行

cd d:\work

first. In my opinion the other solutions miss what the post really wants: simply executing a file like from a DOS or Unix shell, or a .m script in MATLAB. No messing with imports, projects and so on. If you use CTRL SHIFT F10 your code gets executed, sure, but in a different environment, so you have no access to variables created in your code. I assume the question means that you want to do further work with the results of the script.

第一的。在我看来,其他解决方案错过了帖子真正想要的内容:简单地从 DOS 或 Unix shell 或 MATLAB 中的 .m 脚本执行文件。没有搞乱进口,项目等。如果您使用 CTRL SHIFT F10 ,您的代码肯定会被执行,但在不同的环境中,因此您无法访问在您的代码中创建的变量。我认为这个问题意味着您想对脚本的结果做进一步的工作。

Explanation for people with MATLAB background: In most Python IDEs you have to configure an interpreter first in some kind of project. The MATLAB equivalent would be a master IDE where you can choose your MATLAB version for each project. This makes it possible to run your Python code on the CPU, your GPU or even an external NVIDIA board with different settings (after several days in the installation hell). For the beginner this is very confusing, because for simple code samples any "default" interpreter should suffice. Unfortunately this is not the case for Python (2 or 3? 2.x or 2.y? which package version?), and it will get worse as you progress (which 32 or 64 bit version of TensorFlow is available for Python 3.x? and so on).

对有 MATLAB 背景的人的解释:在大多数 Python IDE 中,您必须首先在某种项目中配置解释器。MATLAB 等效项将是一个主 IDE,您可以在其中为每个项目选择 MATLAB 版本。这使得在 CPU、GPU 甚至具有不同设置的外部 NVIDIA 板上运行 Python 代码成为可能(在安装地狱几天之后)。对于初学者来说,这是非常令人困惑的,因为对于简单的代码示例,任何“默认”解释器就足够了。不幸的是,Python 的情况并非如此(2 或 3?2.x 或 2.y?哪个包版本?),并且随着您的进步它会变得更糟(哪个 32 位或 64 位版本的 TensorFlow 可用于 Python 3。 x? 等等)。