[Tutor] format string

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Jul 19 15:50:45 CEST 2006


> It gives following error:
> Traceback (most recent call last):
> File "C:\python\urllib.py", line 1, in -toplevel-
>   import urllib
> File "C:\python\urllib.py", line 9, in -toplevel-
>   page = urllib.urlopen(go_url).read()
> AttributeError: 'module' object has no attribute 'urlopen'


Hi Devanyani,

Change the name of your program to something other than 'urllib.py'. 
What's happening is that your program --- urllib.py --- has the same name 
as the urllib standard library module that you're trying to import.  So 
the program ends up trying to import itself as a module, which is not what 
you want to do.

Python doesn't yet have a nice way to distinguish between your own program 
modules and the standard library's module.  This causes problems from time 
to time, and although it's going to be fixed as a part of PEP 328, that 
fix is still coming.  Until then, you'll want to take special care not to 
name your program with a conflicting name.



> also I tried to assign go_url as
>
>   go_url="http://www.google.co.in/search?hl=en&q=%s&meta=lr\%3Dlang_en"
> %name
>
> It takes the % in "%3D" as a part of  format is there a way I can overcome
> this retaining the same manner of assinging?

You'll want to look into the urllib.urlencode() function.  See:

     http://www.python.org/doc/lib/module-urllib.html

for more details on it.


More information about the Tutor mailing list