Run part of code in a terminal

Sometimes you want to run part of a larger script in a terminal.

Example:
foo=3
type(foo)

What is to do?

Copy the first line of the code manually into the clipboard >
Tools >
Terminal >
type: python >
Enter >
Paste clipboard >
Enter >
Copy the second line of the code manually into the clipboard >
Enter

Many steps.

Is there any way to handle that faster and easier?

And further:
Is there any terminal in PyCharm, which offers syntax highlighting?


Thanks for your help :)
0
5 comments
Avatar
Permanently deleted user
Hi ibu,
PyCharm has special console for Python - it is located at: Tools | Python console. There you have syntax highlighting and with it you can execute parts of your scripts with 'Execute selection in console' action (Ctrl+Shift+E).
0
Thanks Dmitry :)

I selected

foo=3
type(foo)

Than I choosed 'Execute selection in console' from the context menu.

But the result isn't

<class 'int'>

as it appears, when you enter code manually.

Does 'Execute selection in console' only works for single lines only?
0
Avatar
Permanently deleted user
There is a difference in execution multiple statements and single statements in Python. When you execute single statement the result is printed. Multiple statements are executed the same way as they do when you run a script file. So if you write print(_) after you executed the selection you will have <class 'int'> in the console.
0
Thanks for your hint Dmitry
0

The shortcut is Alt+Shift+E on Windows.

0

Please sign in to leave a comment.