Circuitpython "interactive" option - python

I'm trying to debug a circuitpython program (that's running on a microcontroller) and I would like to know if there's a simple way to get the program to drop into the REPL upon a crash/termination while preserving the variables and functions defined in the script.
If this was a regular python program I would simply run it with the "interactive" option of the interpreter set : python -i my_code.py and then have access the variables and function defined in my code for easy debugging.
Instead what I get right now is: after a crash I get prompted to press a key to enter the REPL but the memory is cleared from any trace of my previously running code.

A somewhat cumbersome way to achive an equivalent behaviour, that only works if the code terminates and doesn't crash, is to proceed as follows :
Upload the code
Code will start running automatically
Interupt the code with a keyboard interupt
Press a key to get to the REPL
Import all from the code from the REPL by typing in:
from code import *
Wait for the code to terminate
Finally debug
Rince and repeat for each bug you find...

Related

Shell Closing when starting on Windows and adding new lines

I'm new to code and i have a doubt that i did not find anywhere else. When i try to open a file .py using the Python Shell (double click on Windows) it closes. It's not a code problem because i've created a new file, added this line:
print("test")
and the same thing happended. The most stranger thing is that if i press F5 at the editor, everything works fine. The other thing is that with another file, when i press F5 in the editor, the code works fine but adds a lot of new lines before the code (i did not add any new lines to the code). You can see this code that i'm talking about here and the module here or you can see all the code at GitHub
If you mean you're just double clicking the .py file, you see the shell blink into existence for a moment, then disappear, that's expected. A shell is created to run python to run the script, the script runs to completion, then exits, and the shell (no longer having a running program in it) closes as well.
If you want the output to stick around, either:
Launch a cmd.exe window directly, and invoke your program from there (since the cmd.exe window won't exit, the output will stick around), or
Add a call that prevents the program from exiting until you've had a chance to read the output to the end of your program, e.g. input('Press Enter key to exit...') or the like. As long as that input prompt doesn't get a response, the program is still running, and you can see the output.
The .py file closes immediately because it finishes its execution and exits upon the call of SystemExit. The whole process is too fast for you to be able to see the output on the console. If you want to execute a .py file without using the editor and be able to see the output as well, you can try using cmd and execute a line like this
python yourfile.py
In this way, the window will be left open until you manually close it. However, you have to setup properly to be able to use this python command in cmd, and it is very easy as you can search up online.
For second question, it is because you have printed \n 101 times at line 6 & 7. All function/method calls will be executed at the time you have called them. It does not mean that you can assign a function to a variable, and then only execute the function when you call the variable.
Every function will execute its code and return you something (as what has specified using return statement, can be void too). So if you assign a variable x to a print("\n") statement, it will print \n first and return void(nothing) back to your variable x.

Debugging Python function (or class) from the command line in Spyder

I am a new Python user and I have been programming in Matlab, so I decided to use Spyder IDE (which looks pretty much like Matlab IDE).
Right now I want to debug through (execute line by line in order to understand) some python code that is written as a class with several built-in functions. So, I inserted a breakpoint at the __init__ function of the class, however, when I started a debugging it did not go to the specified breakpoint (since I have to call for a class initialization, rather than just code execution).
Is it possible to start class debugging from the command line?
In Matlab I would just call a function from the command line and it would stop at a specified breakpoint. Here I have to start a debugger, rahter than calling a function. If I simply call the following:
import energy_model
x = energy_model.EnergyModel()
It will just execute and ignore my breakpoint.
Hope my question is clear.
Thanks,
Mikhail
First up, make sure you are hitting the debug button in spyder, not the run button. The run button does not trigger breakpoints, so you will need to hit debug, and then continue to get to the first breakpoint in your code.
If that's failing, one option is to use the python debugger (pdb). This is entirely from the command-line, i.e. running debug commands and receiving debug information will also be through the command-line.
class EnergyModel:
__init__(self):
# set breakpoint
import pdb; pdb.set_trace()
...
Running from the command-line will then pause execution within the __init__ method.
Some of the commands you can issue pdb when the breakpoint is hit are listed here:
https://nblock.org/2011/11/15/pdb-cheatsheet/
Update #1
Example of a function that spyder can trigger breakpoints on
def test(a_string):
print(a_string) # breakpoint set here will be hit
test("hello world")
If you want to debug code in Spyder, it's probably best to run the module it's in by clicking the blue Play/Pause button for debug.
So how do we debug a module that is all classes or functions, with no script? We add script that only runs when this module was the one we clicked Play for, by putting it all under if __name__=="__main__":. (See here for more info on how that works.)
Then we can put a break point in the function or class we want to debug, call that from within if __name__==__"__main__":, run the module with the blue Play/Pause button, and access what's happening from the IPython console.

getch() in Python displaying wrong output

I tried running this code in python
from msvcrt import getch
while True:
char = getch()
print char
but this is displaying the character 'ΓΏ' infinitely.
Can anyone help me with this
Thanks in advance
I don't blame you for wanting to use IDLE for debugging but PDB, while not as convenient, can do anything that IDLE can do and it doesn't have this problem (at least in Windows XP-W10). Under PDB both kbhit and getche work correctly. Whether debugging or not, in W10 your script's first use of getch returns junk before the user presses a key but subsequent use is clean. This isn't necessary in earlier versions of Windows but it is always a good idea to precede your real use with if kbhit() : getch() to ensure a clean buffer before asking for user input.
It just works fine when I run the same program via command line.
I believe you are trying in an IDE? If you are using pycharm, the same query has been posted to them and it is unanswered for like a year.
Link : Using getch() in pycharm
Pls try the same code in command prompt and let us know.
Updated answer :
getch() requires a working "console" window, and when you run Idle you don't get one as its set to use pythonw.exe. Thus, it's technically not able to process any keystrokes and returns immediately. You could try to verify that by starting Idle from the command line with the standard python. (But you may have to be in that console window to trigger the getch() to return)
I personally verified your program by launching IDLE from command line. Well it does not return that character indefinitely atleast. It doesnt return anything for that matter.
Just out of curiosity , do you really need to use IDLE for this program? Wouldn't the regular command line suffice? If yes, am sorry I couldnt be of much help for this question.
To launch idle from command prompt : python C:\Python27\Lib\idlelib\idle.py
Source for the answer : IDLE does not return getch() characters

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

Python shell issue when using Data Nitro

I am using DataNitro to write Python Script in Excel. Its very useful indeed. However, when I open the Idle editor in excel, the accompanying Python Shell is not interactive, in that it does not return print statements, show errors, nothing. It just restarts every time I run the programme. This makes it incredibly hard to debug as I can't use print statements to trace the errors.
Does anyone know if this is a bug with DataNitro, or is it supposed to be that way, or whats going on? are there any solutions?
Thanks so much
Our IDLE editor is just an editor - it doesn't work as a shell.
The best way to debug programs is to raise an exception. This will freeze the shell that opens when a script is run, and you'll be able to inspect the variables and see any print statements that were generated during execution.
For example, if you run:
print Cell("A1").value
x = Cell("B1").value
raise
You'll see the value of A1 printed to the shell, and you can enter "x" at the prompt to see the value of B1.
You can also import a script you're working on into the regular Python shell (the one that opens when you press "shell"). This will execute the code in that script.
We'll be adding a guide to debugging code to the site soon, as well as some features that make it easier.
Source: I'm one of the founders of DataNitro.
Not as knowledgeable as Ben, but have been using DataNitro quite a bit and here are some tips:
The shell automatically closes once the script has run. If you want to inspect some prints or even interact with the shell I normally place following at end of my script.
raw_input("Press Enter to Exit shell")
Not very elegant, but I have even created a small loop that displays text options in the console. Can then interact with your program and sheet from there. Clever and more elegant way would be to have your script poll an excel cell and then take action form there.
Something else that you might find nice is that it also also you to run Ipython instead of the default python shell. Cannot imagine using python without Ipython... so you get benefits of tab completion Ipython debugging etc. To activate that just click the "Use Ipython" Checkbox in DataNitro Settings (don't know if this is version dependent).

Categories

Resources