Custom Bench command

Hello, the only similar topic I found is:

Yet, there was no answer to the question how to make a custom bench command.
I am running latest v7 frappe/erpnext, bench 4.1.0 and by what I found on the net, I was able to write this:

frappe-bench/apps/test/test/commands/ :

from __future__ import unicode_literals, absolute_import
import click
from bench.commands import bench_command

@click.argument('a', None)
def test_importer(a=None):
    print "hello"
    if a:
        print a

commands = [


Still I get error:

root: .... /frappe-bench# bench test_importer "a"
Usage: bench [OPTIONS] COMMAND [ARGS]...
Error: No such command "test_importer".

I also tried with no success:

bench --site site1.local test_importer "a"
bench --site site1.local test_importer --a "a"
bench --site site1.local test_importer -a "a"
bench --site site1.local test_importer
bench test_importer

I have been able to run the code by the following bench command from another file:

bench --site site1.local execute --args "a"

Yet I want to do it by a custom bench command, as the params in execute do not have names for input, while the custom command can give more hints for them.

Tried putting the command in the and I got a lot of errors on bench start. I really do not know what to do here.

Tried with no success to define the custom command in apps/test/test/
Do I have to specify somewhere, which is my command file?
Can anyone give me some pointers?


by going deeper into frappe code, I managed to find:


Still, No idea how to use it or should I use it.


Okey, so there is a connection to the First Edit, but I do not have / should not / must not use it directly.
Instead I should hook up to main function in frappe/utils/
Okey, so far so good. main → get_app_groups → get_app_group → get_app_commands(app_name) function is trying to hook into every app namespace looking for module commands and getting from it commands array! So if I define my command in of commands module of my app, that should do it?

Seems not yet, there is more I am missing.


So… commands in is not an array, it is a list with object/s ? … Okey
Trying to set my commands variable to a list( set( [test_importer] ) ). Keeping in mind that the variable is in the “test” namespace and “commands” module.

And… Nope, not yet.


Finally! I am doing something! Too bad that the server is breaking, but still it’s a change!
So lets see what I’m doing:

apps/test/test/commands/ :

from __future__ import unicode_literals, absolute_import
import click

def test_importer(context):
    print "hello"

commands = {"test_importer": test_importer}

and on bench start I get errors like this one:

Error: No such command "worker".
Usage: bench [OPTIONS] COMMAND [ARGS]...

So, I am overwriting all other commands :frowning:


Judging by the code:

for command in getattr(app_command_module, 'commands', []):
	ret[] = command

commands is a list with functions? Did not know the function has .name :-/
With this I fixed the server start, but again the command does not run.


As revant_one bellow has pointed out, the name of the command has to be with “-” instead of “_”

from __future__ import unicode_literals, absolute_import
import click

def test_importer():

commands = [test_importer]

Too bad it is not working for me. I will continue Monday.

1 Like

I tried to copy the effect of translator app as mentioned in shared post and it worked.

Added file in apps/custom_app/custom_app/

from __future__ import unicode_literals, absolute_import
import click

@click.argument('a', None)
def test_importer(a=None):
    if a:
        print a

commands = [test_importer]

It works

revant@revant-laptop:~/frappe-bench$ bench test-importer 
Usage: bench  test-importer [OPTIONS] A

Error: Missing argument "None".
revant@revant-laptop:~/frappe-bench$ bench test-importer --help
Usage: bench  test-importer [OPTIONS] A

  --help  Show this message and exit.
1 Like

Updated Edit6, above.

Although bench new-custom-command will be clean for users of your app.

There is also the bench execute Read json/csv file(s) from that method.

1 Like

The following code is running smoothly with me.

from __future__ import unicode_literals, absolute_import
from import my_method
import click
import os

@click.argument('path', required=False)
def custom_command(path=None):
    if not path:
        path = "./dir/path/"


commands = [custom_command]

But I have some issue when I call frappe.get_all('Doctype', 'Docname')
in my_method() I get an error says

File "/home/yasin/new-bench/apps/frappe/frappe/", line 1301, in get_all
    return get_list(doctype, *args, **kwargs)
  File "/home/yasin/new-bench/apps/frappe/frappe/", line 1274, in get_list
    return frappe.model.db_query.DatabaseQuery(doctype).execute(None, *args, **kwargs)
  File "/home/yasin/new-bench/apps/frappe/frappe/model/", line 29, in __init__
    self.user = user or frappe.session.user
  File "/home/yasin/new-bench/env/lib/python3.7/site-packages/werkzeug/", line 348, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/home/yasin/new-bench/env/lib/python3.7/site-packages/werkzeug/", line 311, in _get_current_object
    raise RuntimeError("no object bound to %s" % self.__name__)
RuntimeError: no object bound to session

Can anybody help me with this? Or anybody has any idea why I’m getting this error?

I think you are missing connecting to a site. You may have to pass in the site name as an option. This worked for me.

bench start-telethon --site my_site_name

@click.option("--site", help="site name")
def start_telethon(context, site):
    if not frappe.db:

    for d in frappe.get_all("Sales Order", "name", limit=10):