Custom App Python Code lacks Access to Frappe.db

My code is in commands.py in my custom app.

import click
import frappe

@click.command('command2')
@click.argument('params')
def command2(params):
  frappe.db.sql('''
SELECT *
FROM tabDocField
''')
    

commands = [
   command2
]

And here’s the error I get:

Executing get-perm sql query with parameters: params
Traceback (most recent call last):
File “/opt/bitnami/erpnext/frappe-bench/env/lib/python3.8/site-packages/werkzeug/local.py”, line 72, in getattr
return self.storage[self.ident_func()][name]
KeyError: ‘db’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/opt/bitnami/erpnext/frappe-bench/env/lib/python3.8/site-packages/werkzeug/local.py”, line 309, in _get_current_object
return getattr(self.__local, self.name)
File “/opt/bitnami/erpnext/frappe-bench/env/lib/python3.8/site-packages/werkzeug/local.py”, line 74, in getattr
raise AttributeError(name)
AttributeError: db

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/opt/bitnami/python/lib/python3.8/runpy.py”, line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File “/opt/bitnami/python/lib/python3.8/runpy.py”, line 87, in _run_code
exec(code, run_globals)
File “/opt/bitnami/erpnext/frappe-bench/apps/frappe/frappe/utils/bench_helper.py”, line 110, in
main()
File “/opt/bitnami/erpnext/frappe-bench/apps/frappe/frappe/utils/bench_helper.py”, line 20, in main
click.Group(commands=commands)(prog_name=“bench”)
File “/opt/bitnami/erpnext/frappe-bench/env/lib/python3.8/site-packages/click/core.py”, line 829, in call
return self.main(*args, **kwargs)
File “/opt/bitnami/erpnext/frappe-bench/env/lib/python3.8/site-packages/click/core.py”, line 782, in main
rv = self.invoke(ctx)
File “/opt/bitnami/erpnext/frappe-bench/env/lib/python3.8/site-packages/click/core.py”, line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/opt/bitnami/erpnext/frappe-bench/env/lib/python3.8/site-packages/click/core.py”, line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/opt/bitnami/erpnext/frappe-bench/env/lib/python3.8/site-packages/click/core.py”, line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File “/opt/bitnami/erpnext/frappe-bench/env/lib/python3.8/site-packages/click/core.py”, line 610, in invoke
return callback(*args, **kwargs)
File “/opt/bitnami/erpnext/frappe-bench/apps/z1nctl/z1nctl/commands.py”, line 29, in get_perm
frappe.db.sql(‘’’
File “/opt/bitnami/erpnext/frappe-bench/env/lib/python3.8/site-packages/werkzeug/local.py”, line 348, in getattr
return getattr(self._get_current_object(), name)
File “/opt/bitnami/erpnext/frappe-bench/env/lib/python3.8/site-packages/werkzeug/local.py”, line 311, in _get_current_object
raise RuntimeError(“no object bound to %s” % self.name)
RuntimeError: no object bound to db

Because you’re creating a CLI App, you may need to initialize first:

import click
import frappe

frappe.init(site="your_site_name")
frappe.connect()

# the rest of your code
2 Likes

@brian_pond Thanks for replying! How would I go about accessing the site name in the code if I pass it via bench --site mysite {cli commands}? Would I use something like click.options to add it to every command?

At any time, you can retrieve the Site’s name from this object: frappe.local.site

For example:

def hello_there():
    print(f"Hello there!  The name of this Site is {frappe.local.site}")

The 'frappe.local' object stores a lot of useful information. Such as the name of the current user:

print(f"Hello there!  The name of this User is {frappe.local.session.user}")