Hi,
I am creating a script report and want to know a way to debug the python files in console.
Please help.
Thanks!
Hi,
I am creating a script report and want to know a way to debug the python files in console.
Please help.
Thanks!
Since python runs on the server, you cannot reasonably debug it in the browser (apart from frappe.msgprint(interesting_variable)
).
It’s not easy to set up, but maybe Debugging configurations for Python apps in Visual Studio Code can help you get started.
You can use the realtime
function to output it to console. Example:
And on python side:
frappe.publish_realtime("toconsole", data, user=frappe.session.user)
Sure, I got your point. Also, how can we setup the ERPNEXT source code into VSCode? Does it needs to be done on the local server or remote server?
Ideally, a local bench installation. Alternatively, you can set up a development environment on a VPS and use the Remote SSH extension.
Try to use this App
Since everyone is sharing tricks and tips, here’s something I’ve been using for a few years.
In the root module of Frappe App, I created a new function named whatis()
. Whenever I want to know something about an object, I “sprinkle” this in my code. Like this:
customer = frappe.get_doc("Customer", "12345")
postal_code = foo.bar.fetch_postal_code(customer)
# Hmm, I wonder what this object 'postal_code' is all about?
frappe.whatis(postal_code)
doc_address = frappe.get_doc({
'doctype': 'Address',
'address_title': customer.name,
'address_line1': "123 Main Street",
'address_type': "Shipping",
...
The result is a helpful message (both console and msgprint), that tells me about the object. Its value, type, how it was called, line numbers, etc:
I use it all the time.
frappe.whatis(f"Hey Brian, don't forget to review variable 'foo' with value {foo}!")
This silly function saves me the hassle of re-writing the same debugging syntax over and over. Here’s the source, if anyone wants:
import inspect
def whatis(message, backend=True, frontend=True):
"""
This function is using in debugging, and shows an object's value, type, and call stack.
"""
inspected_stack = inspect.stack()
direct_caller = inspected_stack[1]
direct_caller_linenum = direct_caller[2]
parent_caller = inspected_stack[2]
parent_caller_function = parent_caller[3]
parent_caller_path = parent_caller[1]
parent_caller_line = parent_caller[2]
message_type = str(type(message)).replace('<', '').replace('>', '')
msg = f"---> DEBUG (frappe.whatis)\n"
msg += f"* Initiated on Line: {direct_caller_linenum}"
msg += f"\n * Value: {message}\n * Type: {message_type}"
msg += f"\n * Caller: {parent_caller_function}"
msg += f"\n * Caller Path: {parent_caller_path}\n * Caller Line: {parent_caller_line}\n"
if backend:
print(msg)
if frontend:
msg = msg.replace('\n', '<br>')
msgprint(msg)
Thankyou so much, I was able to connect ERPNext’s source code in the VSCode. I am trying to debug trends.py file but it gives me an error. Can you please let me know what am I doing wrong here?
You need to select the correct python from your bench’s virtual environment, where frappe and other apps are installed:
Hi Brian,
in which file is this?
__init__.py
Okay thanks, I was able to configure the virtual environment but trends.py file throws an error. How to fix this?
Are you developing for version 12 or lower? Otherwise you’ll need a higher python version. (Not sure if that’s related to your problem.)