Launch interactive Frappe Python console for debugging and data queries
Launch an interactive Frappe Python console with full database access for debugging, data queries, and executing Python commands. Use it to explore data, fix issues, run migrations, or test API methods on a selected site.
/plugin marketplace add Venkateshvenki404224/frappe-apps-manager/plugin install frappe-apps-manager@frappe-marketplaceLaunch an interactive Python console with Frappe context loaded for debugging, data exploration, and executing Python commands.
Ask user which site to use:
sites/ directorybench use)Offer console options:
A. Standard Console (IPython)
B. One-liner Execution
C. Script Execution
Standard Console:
bench --site [site-name] console
One-liner:
bench --site [site-name] console --execute "print(frappe.db.get_all('Customer', limit=5))"
Script File:
bench --site [site-name] console < script.py
Show common console commands for user reference:
Get Document:
# Fetch a document
doc = frappe.get_doc('Customer', 'CUST-001')
print(doc.as_dict())
Query Database:
# Get all records with filters
customers = frappe.get_all('Customer',
filters={'customer_group': 'Commercial'},
fields=['name', 'customer_name'],
limit=10
)
print(customers)
Execute Queries:
# Raw SQL
result = frappe.db.sql("""
SELECT name, customer_name
FROM `tabCustomer`
WHERE creation > '2025-01-01'
LIMIT 10
""", as_dict=True)
Create Documents:
# Create new document
customer = frappe.get_doc({
'doctype': 'Customer',
'customer_name': 'Test Customer',
'customer_group': 'Commercial'
})
customer.insert()
frappe.db.commit()
Update Documents:
# Update existing document
doc = frappe.get_doc('Customer', 'CUST-001')
doc.customer_name = 'Updated Name'
doc.save()
frappe.db.commit()
Delete Documents:
# Delete document
frappe.delete_doc('Customer', 'CUST-001')
frappe.db.commit()
Test Permissions:
# Check permissions
frappe.has_permission('Customer', 'write', 'CUST-001')
# Set user context
frappe.set_user('user@example.com')
Clear Cache:
# Clear specific doctype cache
frappe.clear_cache(doctype='Customer')
# Clear all cache
frappe.clear_cache()
Reload DocType:
# Reload DocType after JSON changes
frappe.reload_doctype('Customer')
Provide guidance for session:
Important Commands:
exit() or Ctrl+D - Exit consoleCtrl+C - Interrupt current commandhelp(frappe) - Get help on frappe moduledir(doc) - List object attributesTransaction Management:
# Always commit changes
frappe.db.commit()
# Rollback if needed
frappe.db.rollback()
Inspect Objects:
# Get all fields
doc = frappe.get_doc('Customer', 'CUST-001')
print(doc.as_dict())
# Get specific field
print(doc.customer_name)
# Check meta
print(doc.meta.fields)
Trace Errors:
# Get last error
frappe.get_traceback()
# Log error
frappe.log_error('Custom error message')
Profile Code:
import time
start = time.time()
# ... your code ...
print(f"Execution time: {time.time() - start}s")
Count Records:
# Count all
frappe.db.count('Customer')
# Count with filters
frappe.db.count('Customer', {'customer_group': 'Commercial'})
Get Distinct Values:
# Get all customer groups
frappe.db.sql_list("SELECT DISTINCT customer_group FROM `tabCustomer`")
Check Schema:
# Get DocType meta
meta = frappe.get_meta('Customer')
for field in meta.fields:
print(f"{field.fieldname}: {field.fieldtype}")
Bulk Update:
# Update multiple records
customers = frappe.get_all('Customer',
filters={'customer_group': 'Old Group'},
pluck='name'
)
for name in customers:
doc = frappe.get_doc('Customer', name)
doc.customer_group = 'New Group'
doc.save()
frappe.db.commit()
Bulk Delete:
# Delete with filters
frappe.db.delete('Customer', {
'creation': ['<', '2020-01-01']
})
frappe.db.commit()
Test API Methods:
# Call whitelisted method
result = frappe.call('my_app.api.get_customer_details',
customer='CUST-001'
)
print(result)
Run Scheduled Jobs:
# Execute scheduler method
frappe.enqueue('my_app.tasks.daily_cleanup')
Inspect Queue:
# Check background jobs
from rq import Queue
from frappe.utils.background_jobs import get_redis_conn
q = Queue('default', connection=get_redis_conn())
print(f"Jobs in queue: {len(q)}")
Frappe Console Utilities:
Real Console Patterns from Core:
# Migrate data between fields
for doc in frappe.get_all('Item', pluck='name'):
item = frappe.get_doc('Item', doc)
item.new_field = item.old_field
item.save()
frappe.db.commit()
# Fix incorrect values
frappe.db.set_value('Customer', 'CUST-001',
'customer_group', 'Correct Group')
frappe.db.commit()
# Get summary data
result = frappe.db.sql("""
SELECT customer_group, COUNT(*) as count
FROM `tabCustomer`
GROUP BY customer_group
""", as_dict=True)
print(result)
Production Safety:
Transaction Safety:
try:
# Your operations
doc.save()
frappe.db.commit() # Commit on success
except Exception as e:
frappe.db.rollback() # Rollback on error
print(f"Error: {e}")
Permission Context:
# Bypass permissions for admin tasks
doc.insert(ignore_permissions=True)
doc.save(ignore_permissions=True)
# Be careful with this - only use when necessary
# Migrate from old to new structure
for name in frappe.get_all('Old DocType', pluck='name'):
old = frappe.get_doc('Old DocType', name)
new = frappe.get_doc({
'doctype': 'New DocType',
'field1': old.old_field1,
'field2': old.old_field2
})
new.insert()
frappe.db.commit()
# Fix duplicate entries
duplicates = frappe.db.sql("""
SELECT email, COUNT(*)
FROM `tabCustomer`
GROUP BY email
HAVING COUNT(*) > 1
""", as_dict=True)
for dup in duplicates:
print(f"Duplicate email: {dup.email}")
# Sales summary
result = frappe.db.sql("""
SELECT
MONTH(posting_date) as month,
SUM(grand_total) as total
FROM `tabSales Invoice`
WHERE YEAR(posting_date) = 2025
GROUP BY MONTH(posting_date)
""", as_dict=True)
print(result)
# Create user
user = frappe.get_doc({
'doctype': 'User',
'email': 'newuser@example.com',
'first_name': 'New',
'last_name': 'User',
'send_welcome_email': 0
})
user.insert()
user.add_roles('Sales User', 'Purchase User')
frappe.db.commit()
# Update system settings
settings = frappe.get_single('System Settings')
settings.disable_signup = 1
settings.save()
frappe.db.commit()
frappe.db.commit()frappe.db.rollback() if something goes wrong