Using bench execute for testing function

I have made a custom server side scrip and I’m trying to test it by running command ‘bench execute …/frappe-bench/apps/myapp/myapp/script.function’ but I keep getting frappe.exceptions.AppNotInstalledError: App [function] is not installed Error

The command will be like bench execute myapp.script.function

1 Like

Thank you nabinhait. I seem to be making progress after your tip but I’m now getting ImportError: No module named [module]. This module I have already installed using pip install but it fails to import still

1 Like

May be full traceback will help

My full trace back is as follows: I’m using zip and have previously installed it using pip

[frappe@service frappe-bench]$ bench --site mysite execute app.module.function
Traceback (most recent call last):
File “/usr/lib64/python2.7/runpy.py”, line 162, in _run_module_as_main
main”, fname, loader, pkg_name)
File “/usr/lib64/python2.7/runpy.py”, line 72, in _run_code
exec code in run_globals
File “/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py”, line 94, in
main()
File “/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py”, line 18, in main
click.Group(commands=commands)(prog_name=‘bench’)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py”, line 722, in call
return self.main(*args, **kwargs)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py”, line 697, in main
rv = self.invoke(ctx)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py”, line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py”, line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py”, line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py”, line 535, in invoke
return callback(*args, **kwargs)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/decorators.py”, line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/commands/init.py”, line 24, in _func
ret = f(frappe._dict(ctx.obj), *args, **kwargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/commands/utils.py”, line 117, in execute
ret = frappe.get_attr(method)(*args, **kwargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 919, in get_attr
return getattr(get_module(modulename), methodname)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 704, in get_module
return importlib.import_module(modulename)
File “/usr/lib64/python2.7/importlib/init.py”, line 37, in import_module
import(name)
File “/home/frappe/frappe-bench/apps/myapp/myapp/module.py”, line 4, in
from zeep import client
ImportError: No module named zeep
[frappe@service frappe-bench]$

How did you install zeep? Which env?

I think I installed as root meaning in the global env

You need to install it in virtualenv created by bench

add zeep to your requirements file and run
bench setup --requirements

Or

source env/bin activate
pip install zeep

1 Like

This
source env/bin activate
pip install zeep
solved my zeep issue but looks like bench does not like https according to this error

[frappe@service frappe-bench]$ bench --site app.site execute app.module.function
From cffi callback <function _verify_callback at 0x4161668>:
Traceback (most recent call last):
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/OpenSSL/SSL.py”, line 313, in wrapper
_lib.X509_up_ref(x509)
AttributeError: ‘module’ object has no attribute ‘X509_up_ref’
Traceback (most recent call last):

File “/usr/lib64/python2.7/runpy.py”, line 162, in _run_module_as_main
main”, fname, loader, pkg_name)
File “/usr/lib64/python2.7/runpy.py”, line 72, in _run_code
exec code in run_globals
File “/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py”, line 94, in
main()
File “/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py”, line 18, in main
click.Group(commands=commands)(prog_name=‘bench’)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py”, line 722, in call
return self.main(*args, **kwargs)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py”, line 697, in main
rv = self.invoke(ctx)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py”, line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py”, line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py”, line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py”, line 535, in invoke
return callback(*args, **kwargs)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/decorators.py”, line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/commands/init.py”, line 24, in _func
ret = f(frappe._dict(ctx.obj), *args, **kwargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/commands/utils.py”, line 117, in execute
ret = frappe.get_attr(method)(*args, **kwargs)
File “/home/frappe/frappe-bench/apps/zbpayments/zbpayments/payments.py”, line 23, in pickpayments
response=zbbank()
File “/home/frappe/frappe-bench/apps/zbpayments/zbpayments/payments.py”, line 12, in zbbank
client = zeep.Client(wsdl=wsdl)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/zeep/client.py”, line 62, in init
wsdl, self.transport, settings=self.settings)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/zeep/wsdl/wsdl.py”, line 82, in init
document = self._get_xml_document(location)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/zeep/wsdl/wsdl.py”, line 143, in _get_xml_document
location, self.transport, self.location, settings=self.settings)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/zeep/loader.py”, line 75, in load_external
content = transport.load(url)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/zeep/transports.py”, line 110, in load
content = self._load_remote_data(url)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/zeep/transports.py”, line 125, in _load_remote_data
response = self.session.get(url, timeout=self.load_timeout)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/requests/sessions.py”, line 515, in get
return self.request(‘GET’, url, **kwargs)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/requests/sessions.py”, line 502, in request
resp = self.send(prep, **send_kwargs)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/requests/sessions.py”, line 612, in send
r = adapter.send(request, **kwargs)
File “/home/frappe/frappe-bench/env/lib/python2.7/site-packages/requests/adapters.py”, line 504, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host=‘remote.host’, port=443): Max retries exceeded with url: /ServiceWS/remoteService/remoteService.wsdl (Caused by SSLError(SSLError(“bad handshake: Error([(‘SSL routines’, ‘tls_process_server_certificate’, ‘certificate verify failed’)],)”,),))
[frappe@service frappe-bench]$

This was really helpful

Which directory i should be in to execute via bench console?

bench commands must be run from for eg /home/frappe/frappe-bench/ like this:

[frappe@service frappe-bench]$ bench --site app.site execute app.module.function

1 Like

But to confuse and complicate matters further, there’s also a bench console command for eg Bench console how to test python scripts

1 Like

Is this method also true when trying to debug reports?

For example: In my app “repairs” I have a report called “lieferanten_artikelnummer.py” whose main function is “execute”.
by that logic, the statement should be:

bench execute --site site1.local repairs.lieferanten_artikelnummer.execute

However, this just nets a “no module named lieferanten_artikelnummer”.
The report’s location:
frappe-bench/apps/repairs/repairs/repairs/report/lieferanten_artikelnummer/lieferanten_artikelnummer.py

Your command should be like below. It should be according to the directory in which the file is actually placed.

bench execute --site site1.local repairs.repairs.report.lieferanten_artikelnummer.lieferanten_artikelnummer.execute

1 Like

Thanks, this worked. except that for some reason, he doesn’t recognise “–site” anymore. I just omitted the site information and then it worked as it should.

Try this. Site should be mentioned before the command.

bench --site site1.local execute repairs.repairs.report.lieferanten_artikelnummer.lieferanten_artikelnummer.execute

4 Likes