What is a "line marker" icon in PyCharm? - python

What is this "line marker" icon in the gutter in PyCharm IDE?
When does it appear exactly?
I looked up but this doesn't explain much about the icon itself.

It represents the entry point into the application - the line that is first invoked when you run the code.
You have some options when you click on it to run the code in a number of ways, for example with profiling or debugging.

The "Line Marker" (or green run icon) in the gutter is added by PyCharm to special lines the IDE considers as runnable. This is not only the case for lines containing a Top level script enviorment but also frequent with executable test functions from testing frameworks, for example with pytest among others.
The PyCharm IDE detects these automatically (for example by prefixing a function name with test and having a testing framework configurered). One good example of this is the following thread where the IDE makes a mistake detecting a line as runnable, see PyCharm: FooTestCase is no test, but FooTest is.

Related

In VS Code Test Explorer, How to turn off the "play" icon in gutter?

In VS Code, with the Python, Text Explorer, and Python Test Explorer extensions, I am able to unit test great with pytest.
But two of the "gutter" icons interfere with each other, and I cannot figure out how to remove one of them.
Background
There are two different windows for Test Explorer with this setup - one for the base Test Explorer, and one for the Python Test Explorer extension. They work basically the same but I find the organization of the tests better in the Python one, so I use that.
In the test code itself, there are various code lenses applied, and other decorators, that allow you to interact with the tests by running, debugging, opening that test in the test explorer window, etc.
These decorators include an icon that shows up in the gutter, which allows you to run the test, then changes afterward to show success or failure. If you right-click on that icon you get multiple test options, as shown below. There is also a secondary icon showing the status behind the triangle "play" icon in the image below:
The settings for test explorer allow removing the secondary icon in the background (the faint green circle with the checkmark behind the bright triangle "play" icon), with a setting called "Test Explorer: Gutter Decoration". Changing that removes this secondary icon:
Gutter Decorator True:
Gutter Decorator False:
Question
So, my question is - how do I remove the Test Explorer "play" icon from the gutter?
This icon appears to be the main icon connected to the main Test Explorer UI, rather than the Python one (the commands will go to the main UI window and run it's test runs, rather than the Python window). It gets in the way of the status gutter icon behind it that shows the status of the Python test runs and is unnecessary in my setup.
I recently had my PC wiped and I used to have this turned off somehow, but I can't find the setting now. Thanks!
Please try to delete Test Explorer extension. This extension is no longer updated.
It turns out the Test Explorer extension is no longer being updated, per MingJie-MSFT's answer, as it appears the extension was integrated into the main VS Code codebase.
This led me to search the settings again, and I found that most of the settings from the extension (search for "Test Explorer") are now duplicated in another section called "Testing".
So, it turns out the play icon was showing up from the new integrated test explorer, not from the extension. So I was able to shut it off in the new setting:
However, per MingJie-MSFT's answer, the Test Explorer extension is no longer being maintained, so unless the Python specific Test Explorer extension UI was integrated into VS Code as well, at some point this alternate UI (which I prefer some of the design decisions) will likely no longer be available.

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

How to debug in Pycharm

Is there something special I need to debug in Pycharm?
I have the following code:
def main():
print "Hello World"
if __name__ == "__main__":
main()
and I have breakpoints on each. I hit Debug and it gives me some windows and prints both lines even though I have breakpoints on each. Is there any kind of indicator when a breakpoint is hit?
I have seen in other posts something about how you must setup your configuration first? But none of the posts seem to mention how. Here is my configuration
Please do not link me here: https://www.jetbrains.com/pycharm/help/debugging.html I have read it and do not understand what I need to do. I keep getting reffered to lines such as:
In the Project Structure dialog box, configure the roots, dependencies
and libraries to be passed to the interpreter .
I am new to python and am not sure what I should be inputting into the boxes.
So my question is really.
Is Pycharm supposed to debug python out of the box?
How do I setup Pycharm to debug python if not.
I am using the latest python 2.7.9 a fresh install of a few minutes ago.
You set breakpoints in the gutter by clicking a wee bit to the right of the line numbers (if visible)...
...and run under the "Debug" context (it's the green icon that looks like a bug of some kind).
Well it turns out I am just a idiot. Though I have seen people in the debug menu complaining about the same problem.
You are debugging. The breakpoint does not change to indicate you have hit that line like visual studio. Instead one of your lines will change a light blue (depending on your theme) to indicate you are debugging that line.

Is there "Edit and Continue" in PyCharm? Reload code into running program like in Eclipse / PyDev?

Hi all Python developers!
In Eclipse with PyDev it is possible to edit a Python file while debugging. On save, the PyDev debugger will reload the updated code into the running program and uses my new code. How can I do the same thing in JetBrains PyCharm (using Community Edition)?
Eclipse / PyDev writes an output like this when I do that:
pydev debugger: Start reloading module: "MyWidget" ...
pydev debugger: Updated function code: <function close at 0x055F4E70>
pydev debugger: reload finished
I searched settings and web and could not find any hint. Very glad about any idea. Thx.
Edit: I found out in Eclipse/PyDev one has to be in debug mode to be able to use this feature. I tested in PyCharm, but there was no reload done.
PyCharm does not support edit and continue in either the community edition or the professional edition but here is a workaround that I have found while debugging.
Since you can run arbitrary code in the console and/or the expression evaluator, in a lot of cases, you can execute changes to the code without having to restart the application. This isn't exactly like edit-and-continue (which is a feature I really like in Visual Studio and should be part of Pycharm) but it goes a long way towards avoiding having to restart the program from scratch after a change to see if the new code works as expected.
Let me illustrate a couple of the techniques I use:
Let's say you have the following code (with a couple of typos/bugs to illustrate the techniques)
test_value = [10,9,8,7,6,55,4,3,2,1]
for i in range(0,10):
if test_value[i] == i:
print "found the value: " + i
If you run this code, first it errors because you can't print string plus integer but also I wanted to match on 5, not have 55 in the array. So here we go.
Set a break point on the for statement like this and run the code in the debugger.
When it breaks into the debugger, you realize that it should be 5 not 55. Rather than restarting, you can change line 1 to test_value = [10,9,8,7,6,5,4,3,2,1] then select the line, right click and choose Execute Line in Console... which will change the value of test_value to be the array with a 5. Now, the if statement on line 4 becomes true on the value 5. This will then trigger the syntax error on line 5.
Now if you want to make sure you have the syntax correct you can change line 5 to print "found the value: " + str(i), select the line and choose Evaluate Expression... from the right button context menu. When you click Evaluate, the result will show up either in the dialog (or in this case, since it is a print command, in the console)
Now that I've fixed these two issues, I can run the code successfully on the second pass rather than possibly multiple passes it might have taken if I didn't use these techniques. These techniques really pay off if you find a bug deep in the code where it took a while to set up.
Obviously, this is a very contrived example, but hopefully this shows how you can use both Evaluate Expression... and Execute Line in Console... to your advantage while debugging without having to restart your application each time you find a bug in the code.
Also, if you happen to be using Django, PyCharm (professional) will re-launch the server if you make changes to the code. So if you are looking at your web page and notice a problem, you can make a change to the code and switch back to the web page and as you do, either the running application or the debugged application will re-launch and the new code will be running when you refresh the page. Again, not really edit-and-continue but a pretty rapid way to make a change and test.
After all I found a useful and acceptable workaround for my question. It works in PyCharm Community Edition 3.1.2 and I assume it will do in commercial edition as well. I tested on a mid-scale project using Python 2.7.6, PySide (Qt) with one main window and 20+ widgets, tabs, whatever. Follow these steps...
Work in PyCharm on a python project :-)
Execute your code in Debug mode (did not tried Release so far)
Edit some code in one your modules imported during the life of your program
Make your program pause. To achieve this, you can click the "Pause" button of in PyCharms Debug view and then any place in your applications main window where it would need to do something (for example on a tab header). If you have a long a running task and no UI, you may place a breakpoint in a place your program often comes by.
In the Debug view, switch to the Console tab. There is a button on the left Show command line. Click this.
In the console, type in reload(MyModifiedModule) if this call fails, write import MyModifiedModule and try again.
Click resume in PyCharm.
Try the code you fixed.
There are some restrictions on this... It won't fix changes in your main method or main window, cause it won't be created again. In my tests I could not reload widgets from Qt. But it worked for classes like data containers or workers.
May the force be with you as you try this and do not hesitate to add your experiences.
I have the commercial version of PyCharm and just tried testing a simple python script. The script is the following:
for i in range(0,100):
print i
I ran the code in debug mode and placed a break point at the "print i" statement. When the debugger stopped during the first iteration I changed the code to look like this:
for i in range(0,100):
print i
print 'hello'
PyCharm did not reload/re-compile the altered script. Given this simple test my best guess would be that PyCharm does not dynamically reload .py files.
You can add hot reloading features by installing Reloadium plugin.
https://plugins.jetbrains.com/plugin/18509-reloadium
Example use (gif)
It also works without pycharm.
More details:
https://github.com/reloadware/reloadium

Categories

Resources