Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it any cross-platform way to check that my python script is executed under admin rights? Unfortunately, os.getuid() is UNIX-only and is not available under windows :(.

share|improve this question
up vote 3 down vote accepted
import ctypes
import os

    is_admin = os.getuid() == 0
except AttributeError:
    is_admin = ctypes.windll.shell32.IsUserAnAdmin()

print is_admin
share|improve this answer

"Admin rights" is meaningless. Both Unix and Windows have a long list of different privileges that a particular user may or may not have. You should just try to do what you want to do and use exceptions to deal with lack of privileges. That's that they're there for.

share|improve this answer
I need to test if script was executed under "su" on unix/macos and under "run as administrator" under vista+. – Eye of Hell Jun 22 '09 at 11:04
Using exceptions is good, but i need to use third-party applications that have undefined behavior if not run via su/admin, and i do not want to trash a system just to check that script has sufficient rights :) – Eye of Hell Jun 22 '09 at 11:05
I agree asking for forgiveness rather than permission is right most of the time. But as the OP points out, it's not right all of the time – Server Horror Jun 22 '11 at 0:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.