How can I get the actual path of my script?

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?

Text data-block has its linked file path in filepath property. https://docs.blender.org/api/latest/bpy.types.Text.html#bpy.types.Text.filepath

assuming you execute the script from within the Text Editor (recommended)

import bpy
context = bpy.context
text = context.edit_text
filepath = text.filepath
print(filepath)

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)