Pycharm debugging displays variable not available even when a breakpoint is set - python

I am very new to python programming and debugging in pycharm. I want to find the value of a particular variable inside a if condition and have set a breakpoint , however the debugger shows that the variables are not available. Below is the screenshot of the code and the debugger:

I was facing the same issue and I fixed it by going through following steps :-
On the bottom left corner of your pycharm IDE, you will a settings option, click there and then go to "variable loading policy" and set this option to "on demand".
Hope it helps you.

It is because you're code isn't running yet. The print(...) line is inside the function, instead of outside the function. Your code can only run if something outside a function does something.
Fix: Just remove the tab in front of the print statement. Like so:
def factor(n):
# Your function body ...
for (...)
# Somewhere here is the return statement
print(factor(25)) # See the start of this `print` is inline with the `def`

Related

How to view variables in parent scope while using VS Code Python Debug Console

When I set a breakpoint in a function that is imported by another script, I would like to be able to view the variables that are in the parent scope from within the VS Code Python Debug Console. Is this possible, perhaps by modifying launch.json?
Reproducible example:
I have two files, scratch_script.py and scratch_function.py. They exist in the same directory.
I place breakpoints where indicated by comments below.
Contents of scratch_script.py:
import scratch_function
parent_scope_var = 'I disappear'
scratch_function.scratch_function() # breakpoint on this line
pass # breakpoint on this line
Contents of scratch_function.py:
def scratch_function():
child_scope_var = 'I appear'
pass # breakpoint on this line
I run VS Code Python Debugger from scratch_script.py.
At the first break point, I can view parent_scope_var in the Python Debug Console, but not child_scope_var. So far so good.
When I proceed to the second break point, I can view child_scope_var, which is great. But now I cannot view parent_scope_var:
NameError: name 'parent_scope_var' is not defined
On the third breakpoint, the parent_scope_var is seen again in the Python Debug Console, but not the child_scope_var... Which is as I would expect.
So, my only confusion is, why can't I view parent_scope_var when pausing at breakpoints in the called function? The variable still exists, right? Just in the parent scope?
Yes, because when you debug to the second breakpoint, python executes the calling function. At this point, python will open the scratch_function.py file.
First breakpoint:
Second breakpoint:
it will only display the variables that exist in the current file, and there is no parent_scope_var variable in the scratch_function.py file, the python interpreter cannot find parent_scope_var, so the watch panel prompts an error: NameError: name 'parent_scope_var ' is not defined.
But as you might guess, the variable still exists at this point, just inside the scratch_script.py file. At this point, you can open the scratch_script.py file, hover the mouse over the parent_scope_var variable, and vscode will clearly show you the variable value at this time.
On a side note, you can also see the previous parent_scope_var variable in the CALL STACK panel.
PS: a setting to display variable values inline in code files when debugging:
//file:settings.json
{
"debug.inlineValues": "on",
}

debug a function in pycharm

I have a validator function as part of a bigger program which is fifty lines long which returns True or False when you give it a string. For a certain string, it is currently returning False, and I don't which of the many return statements is firing. I can open the Python Console of the interpreter and import the function then give it its argument, but not see on which line it is returning False. Would rather not alter the main program to feed it its argument, would also rather not set breakpoints in the program for this. Is it possible in PyCharm to isolate a function, give it your own custom argument and then step through it line by line?
Am using PyCharm version 2018.2.4
Is it possible in PyCharm to isolate a function, give it your own custom argument and then step through it line by line?
No, it's not unless you create another file and write something like tests there
By clicking on left side of each line you can declare a breakpoint on the line like this:
Then you can go to debug tool window and click on the green play button
More about debuging using PyCharm
Also you can use python's breakpoint()
It is added as a built-in function in Python 3.7 but u can import it to your file on Python's 3.7-

any code in Spyder (editor) of python show me "runfile" in the IPython

I'm starting in Python with Spyder (I just did some work on Jupyter and it was ok ...), but when typing any code in the Editor, the command lines are always shown "runfile ('C: /Users/alexandre.lustosa/ Without title1.py '), instead of the code.
What should I do?
Below is a very simple example of the above problem ...
Thank you!
enter image description here
Well, ok here we go:
The window in the bottom right corner (where the runfile-message appears) is the actual console of python. This is where you type in a command and get output back:
In [1]: print("hello")
hello
In [2]: a = 10
In [3]:
In [4]: print(a)
10
As you can see, just assigning a variable won't return any output, even though python has a=10 internaly saved. To actually get some output which is apparently your aim, you have to call a function which returns something. For example the already built in type() function:
In [5]: type(a)
Out[5]: int
However, you have written your code in the left window, thats not a console but a python file (in your case called titulo1.py), shown by spyder. Spyder knows it's a python-file and therefore highlights the syntax.
When you now click the run icon in the top menu bar, spyder passes your file to the python console in the bottom right corner and the code in the file is then executed.
You could also manually type in the command runfile(filename) in the console. The run-symbol just saves time.
End of story:
Spyder is successfully executing your code, but doesn't return anything, because a variable assignment (a=10) simply doesn't return anything.
Tip:
You can activate the variable explorer of spyder, which allows you to watch all currently assigned variables:

Scrapy Shell: How to execute multiple lines of code in shell?

You will see in the screenshot that pressing enter after pasting a multiline code doesnt run it but merely send each time a "...".
How can I run this multiline pasted code?
someone asked here, but did not get (the right) answer;
Did not work:
Backspace
Use the arrow key to move the cursor, then use the delete key
Escape
F2
Pressing enter twice when inside the Python interpreter executes a block of code, but you have an unmatched open parenthesis on the last line, so you haven't completed defining the block of code. Also, I'm not sure what dic is in the last line, because you haven't included its definition, so you may need to fix that as well.
Running
a=[1,2]
for x in a:
print(x)
actually works (pressing 2 enters worked as expected). So I made a mistake in the code above. I aplogise, I should have checked that before.
I don't delete the question since the one on google can be confusing (the guy did not mentioned it was his mistake, so I though there was a trick to be found. The trick is to check the code).
you could use IPython link which simplifies the process, better yet you have access to every command line as if executed inside the shell.
The other alternative is to encapsulate this inside a function
I know this answer is a bit late, but someone will need the information sometime:
When you make a new line, i.e. title.quote.... you need to press the tab to create an indent, then it will work. Without indenting, you get the "expected an indent" error message.

Prevent "runfile(...)" expression in Spyder console

I'm just getting started with Python and trying to get some easy code-examples to compile. I am using the 'Spyder' Editor and everytime I run code it shows 'runfile(...)' before the actual compiled code in the console.
Is there a way to prevent this behaviour?
Try including this instead immediately prior to your code. The terminal will now return a clean code only response:
cls = lambda: print("\033[2J\033[;H", end='')
cls()
you are trying to run the code, instead go to settings, keyboard shortcuts, and look for "run selection" it will have a shortcut assigned to it
Now select all the code and use the shortcut
it will only give you in and out

Categories

Resources