Running code in PyCharm's console - python

Are there any smooth way to run Python scripts in the PyCharm's console?
My previous IDE - PyScripter - provides me with that nice little feature. As far as I know PyCharm has 2 ways of running script in console:
1) Select a bunch of code and press Ctrl+Alt+E.
2) Save the code in a file and import it from the Console.
Are the any way to do it by pressing "Run" or "Debug" buttons? I need to see the result of my script in the console and all variables available to manipulate.

In the Run/Debug Configuration, add -i to the interpreter options. This will stop it from closing a python session even after a successful run. I.e. you will be able to see all the variable contents

Run -> Edit configuration -> select the script you want to run and give it a display name -> OK
Now you can run it with the green "Run" button. The green bug button (next to the run button) will run it in debug mode.
Remark: next to the run button you can monitor the script/configuration you run by selecting it's display name.

If you create run-time configuration then after pressing the run button (green "play" icon) you will get the desired result: your code will give you output in a console which opens atomatically at the bottom.
You can create many different configuraions for run and debug within a single project.
Here is a link from PyCharm's web help which covers the run/debug configurations:
http://www.jetbrains.com/pycharm/webhelp/run-debug-configuration-python.html
A possible way of manipulating the variables using debug mode and evaluate expression window is added in comment but I missed one detail: to see the result of your interactive code execution you have to switch from Debugger to Console output; mode tabs are on the top-left side of the bottom pane.

The way I do it is a create my script in the editor, call it myfirst.py, eg
print "Hello"
a= 1234
then in the console I run:
reload (myfirst)
To check on the variables use:
>>> myfirst.a
Not perfect but that's pyCharm for you. If you want a pyScripter like experience which I think is more useful you can try spyder2.

It may also be a good option for you to use the magic command
%run scriptname.py
in the ipython console (including the %-character). Compared to Cntrl + Alt + E you also get clean information on errors in your code.
Due to autocomplete it's also typed in a second and you can use this in any environment with an ipython shell. For me as a scientist who often uses python to explore data, this is a good option.

With your program in the editor:
Run - Edit Configurations
Then under Execution check the box for "Run with Python console".
When you run the program from the green arrow it will run in a console. After the run all your objects are available to examine and play with.

Related

VSCode Jupyter: is there a way to make python interactive window work as default console?

I've recently switched to VSCode, and am wondering if there's a way to make the Python Interactive Window from the Jupyter support in VSCode work like the console in Spyder where I just have to select code and press ctrl+enter to send it, without having to create cells everytime.
For now I'm resigned to work with the Terminal until my code is clean and then create a cell when I have reusable code, and would like to just work directly with the PIW.
You can always change the default console setting by:
Opening the Command Palette (⇧⌘P)
Typing "Preferences:Open Settings (JSON)
Edit this line:
"python.dataScience.sendSelectionToInteractiveWindow": false
You should be able to do this with the latest python extension. The select the code you want to execute and press shift-enter. Is that not working?
For me (now) interactive mode runs after setting "jupyter.sendSelectionToInteractiveWindow": true
#FranciscoRZ. You should have seen a popup option for this, but if it didn't come up for you it can just be manually set in VSCode options. Just change this guy here:
Python->Data Science: Send Selection To Interactive Window
That should get you what you are looking for.
OP's Note: Accepting this answer because it will be the right anwser starting with the February release of VS Code Python
If you have the notebook saved as a python percentage script (which is more git friendly)
each "cell" will be delimited by # %% and the default run command is "Shift+Ctrl".
Later once you are working in the interactive window, If you want a particular cell you wrote on the fly to be in you script, there is one button which says "Paste code into file" right next to the recently executed cell in the interactive window.
And in case you are using the notebook for the sake of being able to later export it to html or pdf, once executed in the interactive window, there is an export button as well.

PyCharm: Make 'Execute code in console' stop asking me which console

I have configured PyCharm (or, to be more precise, the selected interpreter) to leave the python console open when a program execution has finished. I find it very comfortable to debug and watch things like in RStudio: Marking them in the source window and hitting Control+Enter (or 'any control like button'+Enter). So after discovering the 'Execute Selection in Console' Command I was able to run stuff interactively in the console the script was ran in. However, there are two issues with that:
1) whenever I do this for the first time, PyCharm asks me in which console I want to execute the code. Then of course I always select 'the console in which the script was run'.
2) Even though I select the console the script ran in, the marked code is always executed in a new python shell (so it forgets about all the pandas settings for example, i.e. it only prints two columns or so)
Can one somehow make it run the marked code always in the console the script runs in?
See the following screenshots:
1) run the script
2) change some of the code (i.e. c becomes aa+2*b instead of a+b), mark it and let it run in the console:
3) PyCharm asks me about 'which console to run the marked code in'???
Oopsie, I found the problem. In the run configuration I did put an argument to the python interpreter (namely '-i' which causes the interpreter to leave the session open even though the script has terminated exactly as I wanted it) but the solution was to let PyCharm do that for you by selecting the 'Run with python console' option:
Now every time I run the script it is run in the same console and I can execute code interactively and PyCharm does not ask me anymore in which console I want it to run.

PyCharm focus Python Console for input upon run

In PyCharm 2018.2.5 (Community Edition), I have bound a keyboard shortcut to "Run File in Console". This launches opens the console and runs the file, but does not focus the console for input and anything I type goes into my code. (Sometimes when I use the switcher to focus the Python console it will try to search console variables when I type.)
Is there a way to automatically focus the Python console for input when it is launched?
No for PyCharm there is no way to automatically focus the Python console for input when it is launched.
In Notepad ++ i can do the following.
Enter my code
press shift+a to execute a macro which saves the file and execute it with Python 2.7 (or 3). I also made this macro a menu item.
When the console runs and the script asks for input you are already in focus and can directly put in our entry without having to click and highlight the window or so as it is necessary in PyCharm.
So the step between writing the code and executing it to see what it does exactly one hotkey. Its not one hotkey and a mouse click or what ever no.... its exactly one press of a button. This makes learning very effective because there are no detours.
I looked long in PyCHarm but i could not find this functionality or a workaround. So the answer to your question is "With PyCharm this is not possible."
Here is how to do it in Notepad ++.
Download Notepad ++
In N++ install the NppExec Plugin.
Create the "save and run in python" macro: In NppExec create a script as shown here:
NPP_CONSOLE 0
npp_save
npp_run cmd /K C:\Python27\python.exe "$(FULL_CURRENT_PATH)"
Save your macro script as say "Save and run in Python Selfmade"
In the hotykeys menu assign your macro to a new hotkey.
In the Npp_Exec menu click on Advanced Options and create a menu entry for your macro. To do this select the script you created under "Associated Script" and then click on "Add/Modify"
Another editor that offers you this quick and direct run functionality is "Atom" with its many plugins.
The shortcut to switch between the code and console is Alt+4; by clicking on the console or using this shortcut, future consoles you open will be selected by the input.
Yes it IS possible
in the run console change the view mode to Window (click the cog)
add the following lines to the benning of your source code
import pygetwindow as gw
win = gw.getWindowsWithTitle('Run')[0]
win.activate()
now it will automatically give focus to the run window... see my youtube demo.. https://www.youtube.com/watch?v=MhmltlUjU3k
i have just posted something related to this. It is needed to distinguish between "Python console" and "python or debug console" within the keymap options. The first one focus at the console but without automatically move caret to it, the later does it.
See more detailes in this answer.
It's very easy! Just make macros with two actions:
1 - Run command,
2 - Activate run tool window. And with keymap in settings create a shortcut. For example, I do this with shift + f10 .

In PyCharm, should I use embedded terminal or running console?

In PyCharm, I have 2 methods to run my code:
press ctrl + alt + F10, then the code will run in running console.
go to embedded terminal, run the code by ./filename.py.
Which method should I use? Or is there a better method? I tried method 1, but problem is some features are missing, like press up arrow for history command, or when use pdb.set_trace() to enter debug mode, the auto-complete feature in running console behaves strangely: for example, in debug mode of running console, when entering a [], the cursor automatically jumps out of the square brackets and do not allow me to type anything in the square brackets. However, there must be a reason why running console exists, right? Otherwise there should only be the embedded terminal.
The better method is to set the "Emulate terminal in output console" option in the settings of the run configuration. You can go to Run | Edit Configurations | Templates and enable the option there so that it will be enabled for all run configurations.
This gives you the higher level of integration of the running console (so that you can run your code using Ctrl-Alt-F10 and not by typing the filename in the terminal) and all features of the terminal emulator. This mode is likely to become default in future versions of PyCharm.

how can I configure PyCharm so that running a Python script file is visible for Python Console

I am writing Python scripts in Pycharm with IPython installed. So I can use Python Console in Pycharm to type Python commands and check the immediate output of the codes. However, when I run a script file after pressing 'Run' button (Shift+F10), all the variables and functions are not visible to the Python Console. This is, however, the feature of Spyder, another popular Python IDE. So here is my question: how can I configure Pycharm so that running a Python script file is visible for Python Console? Thanks
You could also run the part of your code you want to test/check in the console by selecting it and then right clicking and clicking on "Execute Selection in Console Alt-Shift-E". That's what I use sometimes when the debugger is not helpful. After running the code (you can also just "run" functions or classes) the console knows the functions and you can use the same features that Spyder has. However, be aware that when you change the code you need to run it in the console once to update the console definitions!
You can not. But you can use pdb (which will break code execution where you need it and you will be able to do the same things, as in the Python Console).
And, which is better and more powerful, you can use PyCharm's debugger. It represents all available variables in tree-like structures and is really handy.

Categories

Resources