ImportError: __import__ not found when using Server Script on Stock Entry (Frappe v15.69.2)

Hi all — I’m running ERPNext (v15.63.0) and Frappe (v15.69.2) on a self-hosted server.

I’m using a Server Script of type Document Event on Stock Entry, set to trigger After Submit.

Even with only this code

import frappe
from datetime import datetime
frappe.log_error(“Test Script”, “Running”)

I am getting this error on submit…

ImportError: import not found
Possible source of error: Server Script

I have verified:
• Only one script is active
• It’s not disabled
• Other scripts without imports work fine

Is this expected in Frappe v15.69.2? Is there a setting, patch, or known workaround to allow basic imports like datetime?

Thanks in advance.

Server script and other places where dynamic code is loaded and executed…use safe_exec or safe_eval… that restrict the global available in the context… this is security thing…

Thanks — that makes sense.

To follow up, I’ve rewritten my Server Script to remove all import statements and avoided using frappe.cache() or frappe.generate_hash().

I’m now using only:
frappe.utils.now_datetime()
frappe.get_all(“Serial No”, filters=…, limit=…)

The script executes cleanly (no ImportError), but now I’m getting a SQL syntax error when calling frappe.get_all() inside the Server Script:

ProgrammingError: (1064, “You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘’‘\n\n order by tabSerial No.modified DESC’ at line 3”)

Even running this query directly in bench console throws the same error:

Any idea why this is happening? The filters seem clean, and Serial No definitely has a uid field that is nullable. Could this be due to a missing default value or quoting issue in MariaDB?

Appreciate any insights.

I suggest you get it working in System Console first…You can use a denug=True flag there, and maybe get an idea from the query… try something like this in System Console… I can’t see your filters, though it’s unlikely to cause a problem