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

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 :

Related

How to show variables in Python Console after running code in Pycharm?

I am having a problem in PyCharm (2022.3.1) that the variables of my code are not showing in the right-bottom area of "Variables" in Python Console in PyCharm.
This function is extremely useful when coding. You can keep track of the changes and data-type of your variables.
How to solve it?
The problem mentioned was solved and I will described, here, how to do it:
I ran a simple code with two variables. The code runs fine but the variables do not show (in the red circle area, as they should be).
Se this first first screenshot
If I add a new variables manually in the Python Console it will show in the variable area as it shows
So how did I solve this issue?
You will need to change the Debug configurations as shown here, and check the box "Run with Python Console", then Apply, Ok.
After that when you run your code, your variables will be shown in the variables tab at the bottom-right-corner, as shown here.
After that you will also be able to type new variables in the Python Console and they will immediately be shown in the variable tab.

Disable Auto-clearing console (run window) in Pycharm

Is there any way to disable auto clearing console in Pycharm? right now each time that we run the pycharm it clears up the console (run tool window) automatically! I need to have the history of my results
If you want to keep the history of the results, consider writing the output to a file instead of the console.
If you prefer writing to the console (perhaps that's the aim of the application), you can also change the Run Configuration for your script.
In the Run menu, under Edit Configurations..., select the run configuration for your script and select the Logs tab (it shows the Configuration tab by default). On there, you'll find a checkbox to Save console output to file; set a file name in the field next to it.
Currently, PyCharm doesn't have an option to add a timestamp to a filename like this (although the feature has been requested by users, so it may in the future). So, you could add a few lines of code to the start of your program that only runs when debugging, moving the previous output file out of the way of for the new output.

How can I run selected lines in Spyder 4?

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.

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.

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

Categories

Resources