Terminal taking keyboard input when not supposed to - python

When I in a for loop in PyCharm it lets you type in stuff.
Is there a setting to turn that off?
Here's my code
while True:
pass
and it shows

This only happens if you use a run configuration (in other words by clicking one of the green run buttons). It doesn't happen if you run the script directly using the terminal in the OS or by running the script using the Terminal Emulator.
This can be disabled by going to Run > Edit Configurations and checking Emulate terminal in output console. As shown in the screenshot.

Related

How to automatically clear terminal in Visual Studio Code when running the file on terminal?

is there a way to configure VS Code to automatically clear the terminal right before I run a Python file, for example? I searched a lot for that but without success. Whenever I try to run a file in terminal, previous runs are still there in the terminal, and it gets kinda confusing. Notice that I want to clear the terminal when I run the code in terminal (i.e. when I click the play button), not when I run with debugging. My idea is to always have the terminal empty when I run a file. Any thoughts?
Currently in VS Code, it does not automatically clear the terminal data settings, but you could use the command "clear" in the VS Code terminal to clear the previous output data, or click the'kill' icon to delete the current terminal and use the shortcut key Ctrl+Shift+` to open a new VS Code terminal.

Script runs in Pycharm but finished without an exit code

I've been running some simple python scripts in Pycharm as I develop an app. The below described problem happens with every app within this project, while in other projects I'm not seeing this.
When I run the script, even if it's just a simple
print('Hello World.')
I do not get an exit code. It looks as if I am running the program from the Python Console, but I'm running it by right clicking on the script and choosing run.
I can rerun the script without issue, a new tab just opens in the python console window with an incremental (n) next to the filename.
What I end up doing is clicking on the red stop button which is when I do finally get the exit code. I then close the tab and move on.
In your "Run/Debug Configurations" choose "Emulate terminal ..."
Select emulate terminal in debug configs

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.

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.

Running code in PyCharm's console

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.

Categories

Resources