1. Home
  2. Computing & Technology
  3. Python

Getting Login Information With the getpass Module

From , former About.com Guide

Python's getpass module exists for the sole purpose of getting login information from either the user or the environment. For security reasons, it accepts passwords without echoing the characters to the screen. It naturally lends itself to use with networking modules like poplib.

The getpass module does have some dependencies, however, if you use either Mac or Windows systems. To work on Windows systems, it requires the msvcrt module, a module from the Microsoft Visual C++ runtime environment. For Mac, it requires the availability of EasyDialogs (to call EasyDialogs.AskPassword). Unix users will need the termios module; sometimes this is disabled for security reasons. If these are not available when needed, it will throw an error and terminate.

The getpass module provides two basic methods:

  • getpass([prompt]): Prompts the user for a password without echoing the keyboard input to the screen. It then returns the password as a raw string.
  • getuser(): Gets the login name of the user. It first checks the environment variables ($LOGNAME, $USER, $LNAME, and $USERNAME). It then checks the system's password database. If it cannot find a username in any of these, it throws a KeyError. Note that this method is not available on Macs.
It is worth noting that, within each of these methods, one can call the built-in input() function to get input from the screen.

More Python Quick Tips
Explore Python
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Python Library
  5. Python Programming - Python Library Modules - Getting Login Information With getpass

©2009 About.com, a part of The New York Times Company.

All rights reserved.