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

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.

Related

Terminal taking keyboard input when not supposed to

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.

Sending code snippets to the debug console interpreter

I'd like to send code I have selected from the editor to the interpreter running in the debugging console during an active debugging session. Can I do this in VSCode? If so, how?
Update 1
While Mark (accepted answer) provided what seems to be the right command (it works for me from the contextual menu with the mouse), this isn't working for me yet as a keyboard binding, and I reported this issue here.
Update 2
This started working again as of April 21, 2020 (latest Insiders version).
See https://code.visualstudio.com/docs/python/editing#_run-selectionline-in-terminal-repl
Run Selection/Line in Terminal (REPL)
The Python: Run Selection/Line in Python Terminal command
(Shift+Enter) is a simple way to take whatever code is selected, or
the code on the current line if there is no selection, and run it in
the Python Terminal. An identical Run Selection/Line in Python
Terminal command is also available on the context menu for a selection
in the editor.
VS Code automatically removes indents based on the first non-empty
line of the selection, shifting all other lines left accordingly.
Source code that runs in the terminal/REPL is cumulative until the
current instance of the terminal is closed.
The command opens the Python Terminal if necessary; you can also open
the interactive REPL environment directly using the Python: Start REPL
command. (Initial startup might take a few moments especially if the
first statement you run is an import.)
On first use of the Python: Run Selection/Line in Python Terminal
command, VS Code may send the text to the REPL before that environment
is ready, in which case the selection or line is not run. If you
encounter this behavior, try the command again when the REPL has
finished loading.
And see Use IPython REPL in VS Code for info about the IPython REPL.
--------- generic info for other languages -------------------------------------------------------------
It sounds like you want to send it the repl. There is an unbound command:
editor.debug.action.selectionToRepl
which will send selected text to the debug repl.
{
"key": "alt+y", // whatever you want here
"command": "editor.debug.action.selectionToRepl"
},
No, you cannot send a selection of code to the debugger. You need to debug the whole file or use the Interactive Window support to debug a specific cell.

Visual Studio Code - python console

I'm using visual studio code with standard python extension, my issue is that when I run the code the python interpreter instantly closes right after and I only see the output which means that if I create some data structure I have to create it every single time. Is it possible to leave the console open after running the code and maybe running multiple files in the same python interpreter instance?
I used to use spyder which is entirely doing what you want (probably like PyCharm)...
Then I briefly tried VS Code, and it is quite easy to make it behave that way too.
First make sure you have an integrated terminal open (or do Ctrl+`, or View > Integrated Terminal), then in that terminal launch ipython.
Now, when you use the commands from Don Jayamanne's Python extension (Ctrl+Shift+P to open commands palette):
"Run Python File in terminal"
"Run select/line in Terminal"
It will run the line inside the ipython console directly. So running the entire file will call python module.py inside ipython, and thus fails.
So to make this work simply create settings to map which command is executed when "Run select/line in terminal":
Open Language specific settings (Shift+Ctrl+P, search "Configure Language specific Settings...")
Pick up Python
Now I would suggest to make change only in your workspace settings (top-right tab) to keep default behaviour in other cases
so add in WORKSPACE SETTINGS:
(keep in mind it is just a simple/stupid workaround)
{
"python.pythonPath": "run"
}
Now when runing whole file, it will use ipython function run within the ipython terminal that we launched, thus keeping all your workspace variables.
Also, if you run some line of code with "Run Select/Line in Terminal", the ipython session of that terminal keep all variables.
This allows live debugging, without actually going in the debug mode.
When you run a program, it runs until it ends. Then it closes. If you want it to stay live longer, you can make a program which does not stop until told so, e.g.
while True:
something = raw_input('Write something: ')
print('You wrote: %s' % something)
if something == 'bye':
print 'bye.'
break
This will run until user writes "bye".
I'm quite late to this conversation, but a workaround I use is to put a pass statement at the end of my file, then add a breakpoint to it. I then run it in the debugger and can access all of the variables etc.
This allows most of the functionality that I used to use in the PyCharm python terminal, such as exploring data structures, checking out methods, etc. Just remember that, if you want to make a multi-line statement (eg. for a loop), you need to use Shift-Enter to go to the next line otherwise it'll try to evaluate it immediately.

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.

Python and Eclipse: How to use interactive console after running a module, with the module variables and functions

Let's consider 3 situations:
1) I write a pyhon module in Eclipse (pydev) and run it Ctrl-F11. The module runs and I don't have any control or access (AFAIK) to the module variables and functions.
2) I have defined a python interpreter as an external tool in Eclipse, so I can run it within Eclipse. It works fine, but it does not have tab completion.
3) I open my module with python IDLE (not eclipse) and press f5 (run). It runs on the IDLE opened window and when it finishes I have all the variables and functions form the module to play with.
So I have 2 questions:
a) how to enable, if possible, tab completion in python interpreter in Eclipse? If it's not possible, do I have any alternative to standard Python interpreter?
b) I would like to run step 1) and then be able to continue developing / testing with python interpreter, just like I do with IDLE, but all inside Eclipse. Any suggestion?
Thanks in advance
Not sure how much has changed since the question was asked, but it's now possible to get code completion in the debug console (as of PyDev 1.6.0 according to http://pydev.org/manual_adv_debug_console.html, I'm on PyDev 2.8.1, Eclipse 4.3.0)
Set a breakpoint in the code, on an executable statement (even just print('') or True). Double clicking on the grey vertical bar to the left of the code should do it.
Run in debug mode with F11
Eclipse may prompt to switch to the Debug Perspective; either way, the Console should be visible
You should now be able to click in the Console pane and begin interactive debugging with code completion, even after a traceback. (Ctrl+Space works for me.)
Be aware that multi-line statements won't work unless they're split on : or /:
Update in 1.6.0: commands are evaluated on each new line unless the line starts with ' ' or '/t' or ends with ':' or '/' (so, for entering multi-line statements, the input must be entered properly respecting those limitations).
If you run it as a debug operation in Eclipse, you should be able to set a breakpoint, and you can then examine variables, etc. But you can evaluate random python scripts via the watch functionality.

Categories

Resources