Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can PyCharm reuse a console for running code?

Tags:

python

pycharm

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?

like image 701
bjarkemoensted Avatar asked Feb 01 '17 11:02

bjarkemoensted


People also ask

What is the difference between Python console and terminal in PyCharm?

The only real difference is persistence. Console is like a playground for throwaway scripts. On the other hand, your editor allows you to write your code, and save it to a file.

Is PyCharm a Python console?

The main reason for using the Python console within PyCharm is to benefit from the main IDE features, such as code completion, code analysis, and quick fixes. and check the Special Variables list. The console is available for all types of Python interpreters and virtual environments, both local and remote.

Does PyCharm have an interactive console?

PyCharm enables you to use interactive consoles, thus making it possible to stay within the IDE, without the necessity to switch to the shell.


2 Answers

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.

like image 78
kuza Avatar answered Sep 23 '22 17:09

kuza


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.

like image 25
valignatev Avatar answered Sep 23 '22 17:09

valignatev