Running custom scripts via bench console gives invalid syntax error

Hi folks,

I am good upto here like starting console.
bench --site site.name console
A console appears. For the next phase, I am facing somewhat difficulty.

Either, I have not imported the file to be executed successfully or my current path is not traversed properly.

So, moving on please help me with:

1. How to import path of custom python scripts file/folder using bench console?
2. How to execute custom python scripts?

Although, i have went through the syntax like app.function.script. But, unable to make exact replica.

Please do the needful.
Thanks in advance.

1 Like

Hi @kumar404,

where is your custom script (Python, I assume)? Be aware that in ERPNext, custom script refers to JavaScript inserts that can be attached to DocTypes. If you run the console (or in your case, maybe bench execute might even be better), you will be executing server-side Python code.

Assuming your app is apps/yourapp, then your code might be e.g. located in apps/yourapp/yourapp/yourapp/doctype/yourdoctype/yourdoctype.py and might be function def do_stuff. Note that the path is apps/(repo name)/(app name)/(module name)/doctype/(doctype name)/(doctype name).py. So you could run it with

$ bench execute yourapp.yourapp.doctype.yourdoctype.yourdoctype.do_stuff

Hope this helps.

2 Likes

I tried importing the entire module to have access to the functions, but it must be registered as a package.

The only way in Ā“bench consoleĀ“ is like this:

In [1]: from my_app.my_python_module import my_function
And then you run the function:
In [2]: my_function(arguments)

I hope this is useful!

1 Like

Let me add one points.

The directory in which you are working also plays a major role. You should be currently in your custom app directory i think.

Thank you @Tropicalrambler and @lasalesi for your timely response.
This is one stop solution for those who want to make trial and error to run their custom python scripts via console.

1 Like