33

I am trying to use urllib.parse.urlencode() method in one of my scripts. import urllib

#!/usr/bin/python3.2

import urllib

data = urllib.parse.urlencode({'type': 'device_code','client_id': 150792241632891})

It was working before but now I get following error.

Output

Traceback (most recent call last):
  File "/home/rakesh/programming/test.py", line 8, in <module>
    data = urllib.parse.urlencode({'type': 'device_code','client_id': 150792241632891})
AttributeError: 'module' object has no attribute 'parse'

Initially I doubt my python shell but when I checked it is using python version 3.2 which should be fiine.

Now I am totally perplexed why python shell is behaving this way. Am I missing something here?

Thanks

animuson
  • 53,861
  • 28
  • 137
  • 147
Rakesh
  • 3,987
  • 10
  • 43
  • 68

1 Answers1

60

You're not showing the imports in your program, so I can't be sure, but I bet you did

import urllib

which will not import and re-export the separate module urllib.parse. Do

import urllib.parse

instead.

(import urllib is rather senseless in Python 3.x, since all the functionality is in the submodules and these are not imported by the toplevel module.)

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • thanks for responding. Sorry I had missed the import statement while copying the code. Now I have edited my question. you solution worked for me. I guess accidently, I removed _.parse_ part from my import statement assuming that it will include submodule. Since earlier I had import urllib.parse thats why it was working before, but when I remove the submodule it didn't work and I was confuse. – Rakesh Apr 22 '12 at 15:08