No module named Pwd - under Apache 2.2

Peter Otten __peter__ at web.de
Fri Oct 21 05:33:06 EDT 2011


durumdara wrote:

> Hi!
> 
> Win7/x64, Python 3.2, PyPGSQL f 3.2 and Apahce 2.2.
> 
> I created a script that working in CGI mode, it is read some table,
> and returns with an XML.
> 
> It was working with normal mode, under Pyscripter, and under command
> line.
> 
> But!
> 
> When I trying to use it from Apache 2.2 as cgi, I got the subjected
> error:
> 
> "No module named Pwd"

Remember that case matters in Python. Fortunately you included the 
traceback.
 
> I checked the code.
> Everything is fine for the lines
> import postgresql # this is working
> con = postgresql.open(....) # this failed
> 
> Traceback (most recent call last):
> File "C:/web/Apache2.2/cgi-bin/testpg.py", line 20, in Session
> Function()
> File "C:/web/Apache2.2/cgi-bin/testpg.py", line 38, in WebFunction db
> = postgresql.open("pq://postgres:m@localhost/webdbdb")
> File "C:\python32\lib\site-packages\postgresql\__init__.py", line 76,
> in open std_params = _pg_param.collect(prompt_title = None)
> File "C:\python32\lib\site-packages\postgresql\clientparameters.py",
> line 620, in collect cpd =
> normalize(extrapolate(chain(*d_parameters)))
> File "C:\python32\lib\site-packages\postgresql\clientparameters.py",
> line 563, in normalize for (k, v) in iter:
> File "C:\python32\lib\site-packages\postgresql\clientparameters.py",
> line 524, in extrapolate for item in iter:
> File "C:\python32\lib\site-packages\postgresql\clientparameters.py",
> line 130, in defaults user = getuser() or 'postgres'
> File "C:\python32\lib\getpass.py", line 156, in getuser import pwd
> ImportError: No module named pwd

The direct cause is that pwd is not available on Windows:

http://docs.python.org/dev/py3k/library/pwd.html
"""
33.2. pwd — The password database
Platforms: Unix
This module provides access to the Unix user account and password database. 
It is available on all Unix versions.
"""

The source of the failing function...

'''
def getuser():
    """Get the username from the environment or password database.

    First try various environment variables, then the password
    database.  This works on Windows as long as USERNAME is set.

    """

    import os

    for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
        user = os.environ.get(name)
        if user:
            return user

    # If this fails, the exception will "explain" why
    import pwd
    return pwd.getpwuid(os.getuid())[0]
'''

...suggests that you can make it work on Windows by setting the USERNAME  
environment variable (or any of the alternatives checked in the for-loop).

Does postgresql use the os user as a fallback for the database user? If so 
you might consider providing the database user explicitly.




More information about the Python-list mailing list