Import python library

How I can Import external python library by custom script

1 Like

i need too

1 Like

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:

  1. Create a custom application that will be installed alongside Frappe, ERPNext, HRMS, etc.
  2. Add your dependencies (specific non-standard Python libraries) in the pyproject.toml file.
  3. Write the code in a file (that is currently in a server script)
  4. Call the code from outside: from a hook, as a REST API (when frappe.whitelisted), from a server script (with frappe.call), …

Thanks @corentin , could you be kind to provide me more details about those steps?

  1. Creating a new application (do you have some hello world sample)?
  2. I guess it’s ok, I’ve just wrote the code and it is in an main.py file
  3. The entrypoint, instead of main, would be the api function call (I guess I got this one)
  4. 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.