26

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.

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

...how do you do that in pycharm?

user2443457
  • 261
  • 1
  • 3
  • 3
  • 2
    Here's an answer that was more appropriate to me: http://stackoverflow.com/questions/20609581/how-can-i-run-my-currently-edited-file-in-a-pycharm-console-in-a-way-that-i-can – user3107036 Dec 16 '13 at 13:38

9 Answers9

22

Running python scripts using pycharm is pretty straightforward, quote from 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.

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

UPD: Here's an example.

Imagine you have a python module called 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:

>>> 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 Console feature: select the code snippet -> right click -> "Execute Selection in Console".

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I'm able to run the script, but that doesn't (seem) to let me enter additional information into the console in the same way as idle - for instance, calling the function with arbitrary information in order to test it. If instead of run I choose console, I get a console where I can enter information, but none of the functions from the script are available. – user2443457 Jun 01 '13 at 15:36
  • Ok, I see. I've added an example. Please check if it is what you want. – alecxe Jun 01 '13 at 15:48
  • 1
    What you describe is not what OP is asking for and what's available in IDLE. See my answer. – Piotr Dobrogost Jul 12 '14 at 11:35
  • @Piotr Dobrogost sure, this is what the OP already noted in the comment to the question. Thanks. – alecxe Jul 12 '14 at 13:07
  • This isn't the answer to the OP's question. Rampkins below posted the correct answer. – foobarbecue Oct 21 '17 at 04:46
17

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!)

Rampkins
  • 371
  • 4
  • 5
8

Run File In Console

Right Click --> Run File In Console

Done!

enter image description here

StackG
  • 2,730
  • 5
  • 29
  • 45
7

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

flow2k
  • 3,999
  • 40
  • 55
3

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

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
3

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

user3507584
  • 3,246
  • 5
  • 42
  • 66
2

You can run the Find Action shortcut (Ctrl+Shift+A or ++A on mac), then type run file, and choose the option Run file in Console.

enter image description here

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
0

In pycharm do:

Run>Edit Configuration>Show command line afterwards
Nathan B
  • 1,625
  • 1
  • 17
  • 15
0

Assuming your code is in file MySimpleCode.py you can simply say

run MySimpleCode

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

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.

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).