Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python 3.2 error saying urllib.parse.urlencode() is not defined

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

like image 713
Rakesh Avatar asked Apr 22 '12 14:04

Rakesh


People also ask

How do I use Urlencode in Python 3?

In Python 3+, You can URL encode any string using the quote() function provided by urllib. parse package. The quote() function by default uses UTF-8 encoding scheme.

What does Urllib parse Urlencode do?

parse. urlencode() method can be used for generating the query string of a URL or data for a POST request.

Is Urllib built in Python 3?

The urllib module in Python 3 allows you access websites via your program. This opens up as many doors for your programs as the internet opens up for you. urllib in Python 3 is slightly different than urllib2 in Python 2, but they are mostly the same.


1 Answers

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.)

like image 94
Fred Foo Avatar answered Oct 06 '22 06:10

Fred Foo