Try this:
import sys, os
print 'sys.argv[0] =', sys.argv[0] # script name (for testing)
print 'os.getcwd() =', os.getcwd() # where we are (for testing)
print 'script running from', os.path.dirname(sys.argv[0])
If the above truncates the path, try this instead:
import sys, os, string
# walk the system PATH
for d in ( string.split(os.getenv('PATH'), os.pathsep) ):
# if this script (argv[0]) exists in a path directory, it must be the
# script that is executing
if os.path.exists(d + os.sep + os.path.basename(sys.argv[0])):
print sys.argv[0], "is running from", d