Have you noticed that you don't get the error if you run
python ./script.py
instead of
python script.py
This is because sys.argv[0]
will read ./script.py
in the former case, which gives os.path.dirname
something to work with. When you don't specify a path, sys.argv[0]
reads simply script.py
, and os.path.dirname
cannot determine a path.