7

I'm currently trying out PyCharm (Python 3.5 from the Anaconda distribution) and can't find a way to run code in an already open console. As an example, consider the following code:

from time import time
now = time()
from inspect import signature
import numpy as np
import scipy as sp
import sympy
import sys
import matplotlib.pyplot as plt
print(time() - now)

If this is run in the Spyder IDE, the first run takes about 2 seconds, but subsequent runs take microseconds because the modules are already loaded. In PyCharm, every run takes 2 seconds. Is it possible to have PyCharm not reimport modules to run faster?

bjarkemoensted
  • 2,557
  • 3
  • 24
  • 37

4 Answers4

4

My coworker refused to use PyCharm because she was used to this feature in Spyder and didn’t know that it does exists in PyCharm as Execute Selection in Console feature which is typically have a shortcut combination of Alt+Shift+E.

Current PyCharm feature documentation does not mention that short-cut.

Update: As of 2020 documentation is updated and much more comprehensive.

If you have a different keymap you can check in setting window by searching Execute selection in console to see feature’s shortcut or perhaps even assign your own custom short-cut in Settings → Keymap like Ctrl+Enter which resembles interactive console-like experience for many Python developers who like to execute code selections to see outcome during development process.

kuza
  • 2,761
  • 3
  • 22
  • 56
3

According to docs here:

Select chunk of code you want to execute in console, right click and select Execute selection in console or press Enter (enter won't work for vim extension). Every time your selected code will be executed in same console.

valignatev
  • 6,020
  • 8
  • 37
  • 61
  • Thanks, however, this doesn't seem to produce any result. If I select the whole line containing the print function, for instance, and click execute selection in console, nothing actually changes in the console. – bjarkemoensted Feb 01 '17 at 11:20
  • Works for me: first time I see output with somewhat 1.37 sec and other times I see output with 0.00018. It's a silly question but are you sure you selected same chunk of code every time you execute selection in console. Sorry for silly question :) – valignatev Feb 01 '17 at 11:31
  • Ah, I found out what the problem was - I had opened PyCharm by double clicking on my script, and apparently the console only works if you first open PyCharm, then manually navigate to the folder containing the file. This works, but is a major pain, though. Do you know if there's a way to open files just by double clicking and still be able to run snippets in a console like you describe in your answer? – bjarkemoensted Feb 01 '17 at 12:19
  • 2
    In that case you can relaunch console with rerun command (green refresh icon). I personally don't encounter that pain because I never close an IDE :) And btw you can just drag file into Pycharm. – valignatev Feb 01 '17 at 12:31
3

You can go to Settings > Build, Excution, Deployment > Console, then toggle "Use existing console for "Run with Python Console".

This should solve your problem.

astrowq
  • 29
  • 2
0

View -> Tool Buttons (must be checked).

Once done, you should have a toolbar at the bottom of your IDE with the following action available (may depend on running code or debug instances):

4: Run / 6: TODO / Python console / Terminal / ETC.

If you use the python console, you can test code and keep using the console and reload it only when you want.

Unfortunately, the console takes some time to open...

lelabo_m
  • 509
  • 8
  • 21