How to debug MATLAB script when calling it from Python - python

I'm calling a MATLAB script from python using
ml = matlab.engine.start_matlab()
output = ml.myMATLABscript(input1, input2)
The script runs fine in MATLAB, but when I call it from Python it runs into int vs double issues. So far I've been fixing this by interpreting the error message when the script crashes, but it would be nice to see what's going on specifically. For this purpose, I'd like to be able to step through the MATLAB code line for line, even though I've called it from Python.

Turns out this is easier than I expected. Simply programatically set a breakpoint in the MATLAB script. For example:
dbstop if error
Then call the script from Python as before. The MATLAB editor will open in debug mode at the specified breakpoint.
This is also possible without editting the MATLAB script. In that case you need to set the MATLAB breakpoint from Python, using the enginge's eval:
ml = matlab.engine.start_matlab()
ml.eval(dbstop in myMATLABscript if error)
output = ml.myMATLABscript(input1, input2)
For completeness, from MATLAB's documentation:
dbstop in file sets a breakpoint at the first executable line in file.
dbstop in file at location sets a breakpoint at the specified location
dbstop in file if expression sets a conditional breakpoint at the first executable line of the file
dbstop in file at location if expression sets a conditional breakpoint at the specified location.

Related

Problems with redirected stdin and stdout when using py in matlab to run python code

I have some python code that runs perfectly when run within python. When I run the code from within matlab using py I appear to have problems with the redirection of stdin and stdout.
The problem can be illustrated with the python code in a file ztest.py:
import os
def ztest():
os.system(f'./jonesgrid > /dev/null < zxcvbn-split-08.txt')
When run within python, all is well:
>>> import ztest
>>> ztest.ztest()
>>>
When this is run from matlab with the command
>> py.ztest.ztest()
I get the following output:
At line 283 of file jonesgrid.for (unit = 5, file = 'fort.5')
Fortran runtime error: End of file
Error termination. Backtrace:
#0 0x2aaaaacead1a
<other lines cut>
The file fort.5 has been created together with fort.6. Normally these two files are associated with standard input and output respectively and are not created in the run. I have also tried using subprocess.run() and get the same problem.
I'm not sure whether this should be posted in a python forum or a matlab one, but I'm guessing the problem lies with the way in which matlab is interfacing with python. Other parts of my code that use os.system() that don't make use of redirection work fine.

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-

IDLE autocomplete in a new file do not work

If I testing my codes on IDLE the autocomplete its works but if I open a new file don't.
See there pictures below:
I just press CTRL + SPACE.
So.. don't work in this case:
I'll think there are some configuration for solve this, any one knows?
Python idle doesn't work that way. You get autocomplete in idle shell because values are deduced in every run. When you use files your program is not evaluated until you run. Because you can assign any type to a variable at run time, there is no way for idle to confirm the type of variable.
Understand with an example
>> a = dict()
>> a = set()
>> a. # <-- autocomplete knows type of a is set
but the same code in a file
a = dict()
a = set()
a. # <-- How does idle come to know what this variable is without running
but when you run this file once your global variables will show autocomplete feature, but not the local scope variables.
Have you tried saving the script as a *.py file before trying to use IDLE's autocomplete?
More than that, have you considered using a text editor with Python plugins, like Sublime Text and Atom? Or even a python-compatible IDE, like PyCharm, Spyder or even JupyterNotebook.

How does python interpreter run the code line by line in the following code?

I have read that the interpreter runs the code line by line and reports the error if any at the same time and stops the further execution.
So in python, consider the file ex1.py,
print "Hello world"
12variable = 'bye'
print 12variable
Now according to the working of interpreter, the interpreter would run the first line i.e. it prints hello world first and then show the syntax error in the next line (line-by-line working). Hence the expected output is:
Hello world
12variable = 'bye'
^
SyntaxError: invalid syntax
But the actual output is -
12variable = 'bye'
^
SyntaxError: invalid syntax
Why it is not printing Hello World at the first?
It depends on how you run the Python interpréter. If you give it a full source file, it will first parse the whole file and convert it to bytecode before execution any instruction. But if you feed it line by line, it will parse and execute the code bloc by bloc:
python script.py : parse full file
python < script.py : parse and execute by bloc
The latter is typically the way you use it interactively or through a GUI shell like idle.
It's a myth that Python is a fully interpreted language. When CPython runs a script the source code is parsed (this is where it will catch syntax errors), and compiled into bytecode (sometimes these are cached in your directory as .pyc files) before anything is executed. In this regard Python is not all that fundamentally different than Java or C# other than that it doesn't spend much time doing any optimizations, and I believe the bytecode is interpreted one instruction at a time, instead of being JITed to machine code (unless you're using something like PyPy).
Because your understanding of the interpreter is faulty. While it is possible for the behaviour you are describing to occur for a subset of errors it is not the common case for many (most?) errors.
If the interpreter can construct what it thinks is a valid program but there is an error at run time then what you are describing will happen.
Since the case you are pointing at is a syntax error that prevents a valid program being constructed the behaviour is as you see it.
I understand it that way:
Python runs the code line by line after it's in byte code state.
The difference between this thing and compilation (in other languages like C++) is that you have to do this process of interpretation each time you run the script.
Python interpreter interprets the code each time you run the script.
In C++ you compile the program and you can execute it without having to compile it again unless you want to change the system.
Step 1:
The interpreter reads a python code or instruction. Then it verifies that the instruction is well-formatted, i.e. it checks the syntax of each line. If it encounters an error, it immediately halts the translation and shows an error message.
Step 2:
If there is no error, i.e. if the python instruction or code is well-formatted then the interpreter translates it into its equivalent form in an intermediate language called “Byte code”.Thus, after the successful execution of Python script or code, it is completely translated into Byte code.
Step 3:
Byte code is sent to the Python Virtual Machine(PVM).Here again, the byte code is executed on PVM. If an error occurs during this execution then the execution is halted with an error message.
So in your case, the "invalid syntax" error is thrown because of step1. But, the actual print function gets executed at step 3. step 3 comes only after step 1 right... I think got it now.

Python: Is line by line execution possible

Is it possible to run code line by line with Python.
Including running any module code, when used, line by line as well.
I would like to go out and run some code line by line and watch as each of the lines goes through the processing phase and see just what code is getting executed when certain actions occur. I'm curious how certain values are getting passed off to the interpreter.
Just use python -m pdb mycode.py, which will run your code in the python debugger (pdb module).
In the debugger you can execute arbitrary code, watch variables, and jump to different places in the code. Specifically, n will execute the next line and h will show you the debugger help.
To add a breakpoint you can use pdb inline:
import pdb; pdb.set_trace()
From this line bleow you can go step by step and you can enter inside every called function (with s) or jump function execution (with n).
Also, a good tip is to use ipdb instead of pdb because it knows autocomplete (it's an ipython prompt)

Categories

Resources