How can I run selected lines in Spyder 4? - python

In previous versions of Spyder, you can select lines in the editor, and only run those selected lines.
In Spyder 4, when you select lines and press Ctrl+Enter it executes runcell(0, '/your/dir/file.py') which runs the whole code.
How can I run just the lines which I have selected?

The hotkey for running a the line at which the cursor is, or the currently selected lines in Spyder 4 is F9. Ctrl+Enter will run the current cell. You can read more about cells in Spyder here. Basically, these are blocks of code that can be defined in the editor using a specific syntax within the code, and can be run independently by Spyder and other IDEs.
Incidentally, Ctrl+Enter is the keyboard shortcut for running the current line in R Studio, not Spyder, which may be where you got it confused.

In Spyder 4.0.1, the keyboard shortcut for 'run cell' is set to Ctrl+Return but for 'run selection' it is set to F9. You can use F9 to run a selection or if you prefer to use Ctrl+Return, you can go to Tools -> Preferences -> Keyboard shortcuts. Search for 'run selection', double click and set Ctrl+Return as the 'New shortcut'

In Spyder, you can organize your code as runnable cells
Use the the pattern # In[] or #%% to define the start of a cell. You can also have child cells by adding additional percent signs %

In Spyder 4 the keyboard shortcut to run the highlighted lines of code is F9. and if you want to use any other shortcut then you can change it from TOOLS>>PREFERENCES>>KEYBOARD SHORTCUTS then double click on any shortcut you want to change and change with your shortcut. but be very careful if you used the same shortcut that is used for any other work then for that it will remove.

Related

Run selected lines and show output using jupyter notebook

I was previously using the Jupyter extension for VS-code (https://github.com/DonJayamanne/vscodeJupyter) and could select my code and run it with the output being displayed in a 'Results' pane.
This no longer seems to work - when I for instance select a variable and run it the "Python 3 Kernel" status at the bottom flicks rapidly between busy/idle, but the variable is not displayed.
I see that the extension is now deprecated and instead one can run cells using the python extension, however is there any way to replicate this old functionality which was quite central to my workflow?
So we actually just added this functionality to the Microsoft Python Extension and it just shipped last night. If you are in a .py document with #%% cells defined you can hit shift+enter in a cell with no text selected to run that cell in the Interactive Windows. If you are in a .py file with cells defined and you selected text and hit shift+enter then just that text (not the whole cell) will get sent to the Interactive Windows. If you are in a .py file without cells defined and you select text and hit shift-enter that text will get sent to the normal python terminal as before. But you will now see a one time pop-up asking if you would instead like to send shift-enter commands in non-cell files to the Interactive Window instead. If you miss the pop-up you can also just change the Send Selection to Interactive Window command that we had added in the options.

Copy/Paste from Python command line to sublime

Apologies in advance for what should seem obvious to me...
New to Python, but other wise very experience, hence the frustration.
As I'm learning, I'm attempting to prototype the command at the command prompt (">>>") as I script. I can enter the python commands and validate they are correct, but (currently) then have to retype the command in to sublime, which seems inane, especially as I'm watching videos where they are copying and pasting one to the other...
Windows 7 CMD, & Python ">>>" command lines, Sublime window
I'm trying to copy commands from the Python command line ">>>" to the sublime editor in an open/new tab. Normally, I would mark/highlight the selection, then Ctrl+c, place the cursor in the target window (sublime in this case), Ctrl-v, and be done.
However, I'm unable to select what I want to copy (??). Up/down arrows will recall the individual lines (no problem with that) -- where I would usually do a Ctrl-(left/right arrow) to select the text, but no indication that anything has been selected. Same result using the mouse.
Thanks in advance for the assist.
This is an oddity of the Windows command prompt. First, there is a little bit of setup. Then you can do the cut-and-paste in a slightly different way than you are used to.
Setup
Right click on the title bar at the top. Select "Properties". Click on the "Options" tab. Make sure that the "QuickEditMode" box is checked. Close the Properties window.
Cut-and-paste should be enabled, but not using control-C to cut. Select the text you want to copy. Instead of typing control-C, just hit return (with the desired text selected). The text should now be on the clipboard to paste into other applications (with control-V)
In Linux terminals, pressing Ctrl+k copies everything in the line that follows the cursor. Ctrl+u copies the everything before the cursor.

How to run only block of code in PyCharm like in Spyder?

In Spyder I can run only a part of code without running everything. I know that in PyCharm I can click right mouse button and "Execute Selection in Console", but it will be new execution without values and variables which declared before this part of code.
So, very often I need to run only few last lines of my code, with parametres and options that I already have.
you can try to use the interactive interpreter while in debug mode.
I find it to be very useful when trying to run code snippets in the program.
view the screenshot below.
You can use the "Run cell" functionality and the cell will be executed in the Python Console (the same way it does when right clicking "Execute Selection in Console"). To enable that I am aware of two options :
In settings (Ctrl+Alt+S) install the "PyCharm cell mode" plugin.
Then use ## to create code sections.
https://plugins.jetbrains.com/plugin/7858-pycharm-cell-mode
Create a new project in scientific mode (only available in PyCharm professional)
You can create code cells with #%%.
https://www.jetbrains.com/help/pycharm/matplotlib-support.html
In both cases, it creates cells that you can execute with the green "play" button like shown below :

shortcut to run current Python unit test in VSCode

The question is regarding Visual Studio Code (VSCode from here) and python VSCode extension that finds and runs py.test tests.
Is it possible to assign some shortcut to run current (under cursor) test method and/or test class?
I really like the ability to run single test straight from VSCode, but my workflow is not optimal since it is necessary to click it and just use some shortcut.
This image shows the buttons that appear when tests are found and can be run. I would like to know if it is possible to just use some custom shortcut instead of clicking this buttons.
Try Test Explorer UI and Python Test Explorer for Visual Studio Code.
The command list:
However, run-test-at-cursor doesn't work for me. I use run-file. Moreover, maybe you should join workbench.action.files.save, test-explorer.reload, and test-explorer.run-test-at-cursor with a macro extension.
Go to File > Preferences > Keyboard Shortcuts
In the searchbox, type python
All Python commands will be displayed below.
Select the command you want to add a key binding and click on the '+' button. Enter your preferred key combination in the new window.
Starting with VSCode 1.59 and the new Testing API (?) there are now these two new commands:
Run Test at Cursor
Debug Test at Cursor
Assuming the unit test you want to repeatedly run is selected in the Test sidebar (which you can do just by clicking on it), I've found a keyboard-driven option that doesn't require an extension to repeat running it. From anywhere in VS Code, type:
shift+cmd+i, which for me is bound to the command workbench.view.extension.test.
(Optional) Note that annoyingly, you sometimes have to press shift+cmd+i a second time to select the unit test in question (this seems like a bug to me - this happens with all Side Bar views)
Tab-Tab-Space (i.e. Tab, then Tab, then Space). The two tabs select the little bug symbol on your test and the Space kicks off another debug run of the selected test.
Here's how the screen should look after the two Tab presses:
and then the Space "clicks" the selected debug button.
With these 2-3 steps you can repeat this unit test ad infinitum.
HTH
In my case, a lot of test functions already have shortcuts:
I also added a shortcut to get to the test bench with the keyboard shortcuts utility.
Run/Debug/Interactive console in VSCode Editor ==>
File -> Preferences -> Extensions -> Robot > Code Lens: Enable/Disable
File -> Preferences -> Extensions -> Robot: Variables -> Edit in settings.json
need to add "ENV":"DEV"
Restart VS Code editor

Is there a Python shortcut to circumvent need for `print()`

In R 3 * 2 typed on the editor can be executed in the console as [1] 6 by having the cursor on the line where the code is typed; clicking on Run if using RStudio, or through Ctrl + Enter. Very convenient.
New to Python, I am coming to realize the if I want to see 6, I may need to type print(3 * 2), unless I type the expression directly on the Python console. Or, is there a shortcut?
Incidentally, I am using Pycharm as IDE.
In the Pycharm charm editor go to
Settings > Keymap > Other
And change the kep map for "Execute selection in console". Double click it and select "Add keyboard shortcut"
I think the default is set the Alt+Shift+E. I was also from an R background before Pycharm and was used to the shortcut of Ctrl+R to run selected code. I think Ctrl+R might be something in Pycharm because I decided a long while back to map mine to Alt+R.
Once this is done, you can highlight a section and use your new shortcut to run it in a console. You can also just have a cursor on the line and using the shortcut will run the line and move to the next.
You need IPython. In particular, this ability to select a section of a saved file and re-execute it with a click or keypress is the hallmark of the Jupyter interface (previously known as "IPython Notebook").

Categories

Resources