Running a module from the pycharm console
Asked Answered
A

9

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?

Aceous answered 1/6, 2013 at 15:4 Comment(1)
Here's an answer that was more appropriate to me: #20610081Plutonium
P
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".

Pinnace answered 1/6, 2013 at 15:28 Comment(5)
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.Aceous
Ok, I see. I've added an example. Please check if it is what you want.Pinnace
What you describe is not what OP is asking for and what's available in IDLE. See my answer.Disappoint
@Piotr Dobrogost sure, this is what the OP already noted in the comment to the question. Thanks.Pinnace
This isn't the answer to the OP's question. Rampkins below posted the correct answer.Letta
K
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!)

Keneth answered 2/4, 2015 at 16:39 Comment(0)
M
8

Run File In Console

Right Click --> Run File In Console

Done!

enter image description here

Morpho answered 31/10, 2018 at 14:54 Comment(0)
F
7

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

Faenza answered 21/1, 2019 at 21:51 Comment(0)
D
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.

Disappoint answered 12/7, 2014 at 11:31 Comment(0)
K
3

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

Kerk answered 4/1, 2015 at 23:33 Comment(0)
S
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

Sangria answered 16/4, 2019 at 5:14 Comment(0)
P
0

In pycharm do:

Run>Edit Configuration>Show command line afterwards
Photocopy answered 20/12, 2018 at 20:50 Comment(0)
O
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).

Orgy answered 22/6, 2019 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.