Say I have a Blender file located at C:\blender\BlenderFile.blend. The project contains a script linked to a file located at C:\scripts\script.py.
The call os.path.realpath(__file__) in this script returns C:\blender\BlenderFile.blend\script.py. How can I find the original filepath C:\scripts\script.py in the script instead?
using __file__ in case you want to execute the script from any context (in which case, you might be doing whatever you do suboptimally. i do not recommend using this method, unless you know what you are doing)
import bpy
from pathlib import Path
context = bpy.context
text = context.blend_data.texts[Path(__file__).name]
filepath = text.filepath
print(filepath)