问题 I'm trying to get the name of the Python script that is currently running. I have a script called foo.py and I'd like to do something like this in order to get the script name: print Scriptname 回答1: Use __file__ . If you want to omit the directory part (which might be present), you can use os.path.basename(__file__) . 回答2: import sys print sys.argv[0] This will print foo.py for python foo.py , dir/foo.py for python dir/foo.py , etc. It's the first argument to python . (Note that after py2exe