Running a module from the pycharm console

69,271

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

Run File In Console

Right Click --> Run File In Console

Done!

enter image description here

Solution 5

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.

Share:
69,271

Related videos on Youtube

user2443457
Author by

user2443457

Updated on June 23, 2020

Comments

  • user2443457
    user2443457 about 4 years

    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
    user2443457 about 11 years
    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.
  • alecxe
    alecxe about 11 years
    Ok, I see. I've added an example. Please check if it is what you want.
  • Piotr Dobrogost
    Piotr Dobrogost almost 10 years
    What you describe is not what OP is asking for and what's available in IDLE. See my answer.
  • alecxe
    alecxe almost 10 years
    @Piotr Dobrogost sure, this is what the OP already noted in the comment to the question. Thanks.
  • foobarbecue
    foobarbecue over 6 years
    This isn't the answer to the OP's question. Rampkins below posted the correct answer.