No module named Pwd - under Apache 2.2

D

durumdara

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"

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 Apache is running under my account (normal user).
The sys.path is ok:
['C:\\web\\Apache2.2\\cgi-bin', 'C:\\Windows\\system32\\python32.zip',
'C:\\python32\\DLLs', 'C:\\python32\\lib', 'C:\\python32', 'C:\
\python32\\lib\\site-packages']

So we (me, and the postgresql's author) don't understand, why it
happens.

Any idea?

Thanks for your help:
dd
 
P

Peter Otten

durumdara said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top