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, …

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 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!!!

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 file, for example in doc_events:

In a server script, it’s also possible to call your function if whitelisted with:"my_app.main.lorem_ipsum")

but I would NOT recommend doing that.

Thanks @corentin , why do you not recommend? security issues?

why do you not recommend?

I do not recommend to combine in a server script in combination with a custom app: what is possible with server scripts is also possible in a custom app (with 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 :woozy_face:

But it’s possible, it’s just not worth it when you already have a custom app.