Newbie: A very basic problem related to cgi.py

A

Ali

Hello all,

I have got a very simple python code:
___________________________

#!/usr/bin/python
import cgi

def main():
print "Content-type: text/html\n"
form = cgi.FieldStorage()
if form.has_key("firstname") and form["firstname"].value != "":
print "<h1>Hello", form["firstname"].value, "</h1>"
else:
print "<h1>Error! Please enter first name. </h1>"

main()
__________________________

I try to run this form the command line (I am on linux) just to check
it out and it gives me the following errors/output:
__________________________

Content-type: text/html
Traceback (most recent call last):
File "./mycgi.py", line 2, in ?
import cgi
File "/usr/lib/python2.3/cgi.py", line 12, in ?
"""Support module for CGI (Common Gateway Interface) scripts.
File "/usr/lib/python2.3/cgi.py", line 6, in main
# scripts, and /usr/local/bin is the default directory where Python
is
AttributeError: 'module' object has no attribute 'FieldStorage'
__________________________

Is it reasonable to infer from the above that cgi.py module has been
loaded successfully? because if it is, why on earth am i getting the
error for FieldStorage??

I think i am missing something very basic here... plese enlighten...
Thanks
 
A

Ali

Okay, i don't know what the problem is...

I ran python in interactive mode and tried "import cgi.py" and i got
the same error...

Any ideas?
 
S

Steve Holden

Ali said:
> Hello all,
>
> I have got a very simple python code:
> ___________________________
>
> #!/usr/bin/python
> import cgi
>
> def main():
> print "Content-type: text/html\n"
> form = cgi.FieldStorage()
> if form.has_key("firstname") and form["firstname"].value != "":
> print "<h1>Hello", form["firstname"].value, "</h1>"
> else:
> print "<h1>Error! Please enter first name. </h1>"
>
> main()
> __________________________
>
> I try to run this form the command line (I am on linux) just to check
> it out and it gives me the following errors/output:
> __________________________
>
> Content-type: text/html
> Traceback (most recent call last):
> File "./mycgi.py", line 2, in ?
> import cgi
> File "/usr/lib/python2.3/cgi.py", line 12, in ?
> """Support module for CGI (Common Gateway Interface) scripts.
> File "/usr/lib/python2.3/cgi.py", line 6, in main
> # scripts, and /usr/local/bin is the default directory where Python
> is
> AttributeError: 'module' object has no attribute 'FieldStorage'
> __________________________
>
> Is it reasonable to infer from the above that cgi.py module has been
> loaded successfully? because if it is, why on earth am i getting the
> error for FieldStorage??
>
> I think i am missing something very basic here... plese enlighten...
> Thanks
>
It's actually something that's quite difficult to deduce from the
traceback, for reasons that will (I hope) shortly become clear.

Here's what I saw when I ran your program:

sholden@dellboy ~
$ python test98.py
Content-type: text/html

<h1>Error! Please enter first name. </h1>

However, look what happens when I rename the same file to "cgi.py" and
then repeat the experiment:

sholden@dellboy ~
$ mv test98.py cgi.py

sholden@dellboy ~
$ python cgi.py
Content-type: text/html

Traceback (most recent call last):
File "cgi.py", line 2, in ?
import cgi
File "/c/steve/cgi.py", line 12, in ?
main()
File "/c/steve/cgi.py", line 6, in main
form = cgi.FieldStorage()
AttributeError: 'module' object has no attribute 'FieldStorage'

Ha! There's a naming confusion between the imported cgi module and your
main program. Rename it to something else and all should be well (or at
least better ;-)

regards
Steve
 
P

Peter Hansen

Ali said:
Okay, i don't know what the problem is...

I ran python in interactive mode and tried "import cgi.py" and i got
the same error...

Any ideas?

Probably nothing really helpful, I'm afraid. Still, try
these ones out:

1. When you tried "import cgi.py" from the interactive prompt,
was that really what you typed? Because that's not a valid
import statement for the cgi module. You should have done
"import cgi" instead.

2. If you really did "import cgi", and didn't get an error
at that instant, then the module really does exist and
can be imported. In that case, typing "dir(cgi)" would
tell you what names were present in it. That might give
you a hint as to the solution.

3. There's something strange about that traceback, I
think. Why does it show the doc-string for the module,
and why does it show a comment after the "line 6" line?
Did you hand-edit this before posting it?

4. Standard library modules like cgi are written in pure
Python, so the source is right there on your machine.
Try "less /usr/lib/python2.3/cgi.py" and spend some time
looking at that module, and see whether anything looks
fishy. On my machine, for Python2.3, I see that cgi.py
does a few imports, defines an "__all__" list which by
the way defines which symbols will be "exported" if you
do "from cgi import *", and then defines a bunch of
functions and classes, one of which is clearly FieldStorage.
Unless your Python installation is broken, I would think
you'd have the same and that the whole thing couldn't
really go wrong... it does look like strange behaviour,
but that's usually a sign that the solution will be
quite simple, maybe of the forehead-slapping kind.

-PEter
 
P

Peter Hansen

Steve said:
Traceback (most recent call last):
File "cgi.py", line 2, in ?
import cgi
File "/c/steve/cgi.py", line 12, in ?
main()
File "/c/steve/cgi.py", line 6, in main
form = cgi.FieldStorage()
AttributeError: 'module' object has no attribute 'FieldStorage'

Ha! There's a naming confusion between the imported cgi module and your
main program. Rename it to something else and all should be well (or at
least better ;-)

I suspected that too, Steve, but why would it be showing
the library cgi.py in the traceback if he had a local
one with the same name? Weird. (Unless, as I wondered
in my redundant reply, the traceback he posted wasn't
the real traceback...)

-Peter
 
A

Ali

You are right, I had first named my file cgi.py... but then i realized
that it was causing conflicts so i renamed it to mycgi.py as you can
see in the trace, but the problem persisted...

After reading your reply i took a look at the directory i was in and
there was a pyc file in it... cgi.pyc, i removed it and it worked!
Thanks for the reply, it pointed me in the right direction...
 
A

Ali

1. When you tried "import cgi.py" from the interactive prompt,
was that really what you typed? Because that's not a valid
import statement for the cgi module. You should have done
"import cgi" instead.

import cgi was giving me the same error, because it was importing from
the cgi.pyc?
3. There's something strange about that traceback, I
think. Why does it show the doc-string for the module,
and why does it show a comment after the "line 6" line?
Did you hand-edit this before posting it?

nope, i didn't edit anything...
maybe of the forehead-slapping kind.
Thanks for the reply, yes it was :)
 
S

Steve Holden

Peter said:
I suspected that too, Steve, but why would it be showing
the library cgi.py in the traceback if he had a local
one with the same name? Weird. (Unless, as I wondered
in my redundant reply, the traceback he posted wasn't
the real traceback...)
It *is* a little puzzling.

I suspect that the problem might have been due to repeated imports or
some such. But I don't know. Only Ali can tell us how he got that
traceback. I wondered whether it could have been an IDE, but neither
IDLE nor PythonWin exhibit such strangeness. Very odd. Ali?

regards
Steve
 
P

Peter Hansen

Steve said:
It *is* a little puzzling.

And answered: the code that prints tracebacks does a
search for source code by scanning sys.path, I suppose,
rather than looking at the __file__ attribute of the
failing module. That means when it failed to import
his cgi.pyc file properly, it went looking for cgi.py
and found it in sys.path, in the standard library,
and printed out line 6, which happened to be part of
a doc-comment, not real code.

Unexpected at best... really confusing to a newbie (let
along us big-ego types) at worst.

-Peter
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top