How I can Import external python library by custom script
i need too
me too
It is currently not possible to import a Python library in Server Scripts, mainly for security reasons.
However, it is possible to import anything inside a custom application. You have to:
- Create a custom application that will be installed alongside Frappe, ERPNext, HRMS, etc.
- Add your dependencies (specific non-standard Python libraries) in the pyproject.toml file.
- Write the code in a file (that is currently in a server script)
- Call the code from outside: from a hook, as a REST API (when
frappe.whitelist
ed), from a server script (withfrappe.call
), …
Thanks @corentin , could you be kind to provide me more details about those steps?
- Creating a new application (do you have some hello world sample)?
- I guess it’s ok, I’ve just wrote the code and it is in an main.py file
- The entrypoint, instead of main, would be the api function call (I guess I got this one)
- How to call the api from the other app, installing alongside with frappe will make it visible automatically?
Any further help will be trully appreciated. thank you!!!
https://frappeframework.com/docs/user/en/tutorial/create-an-app
@frappe.whitelist()
def lorem_ipsum():
import my_external_module
frappe.only_for(["System Manager", "Accountant"]) # ensure permissions if you then use ignore_permissions
print("Hello, World!")
Then, you can directly specify it in the hooks.py
file, for example in doc_events
:
In a server script, it’s also possible to call your function if whitelisted with:
frappe.call("my_app.main.lorem_ipsum")
but I would NOT recommend doing that.
why do you not recommend?
I do not recommend to combine frappe.call
in a server script in combination with a custom app: what is possible with server scripts is also possible in a custom app (with hooks.py). However, when combining both, then your server script becomes dependent on your app, and vice-versa. You cannot update one without maybe updating the other, even for small changes: you would have to update both at the same time, which can become a huge maintenance burden.
For example, adding a parameter to your Python function means that you also have to update the script that calls this function. So, you need to update the server script code just after doing bench update
But it’s possible, it’s just not worth it when you already have a custom app.