0

ModuleNotFoundError: No module named 'cookielib' and 'urllib2'

import urllib, urllib2, cookielib

username = 'yourusername'

password = 'yourpassword'

ck = cookielib.CookieJar()

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

loginn = urllib.urlencode({'username' : username, 'j_password' : password})

opener.open('https://login.facebook.com/login.php?login_attempt=1', login_data)

resp = opener.open('http://www.facebook.com/')

resp.read()

print(resp) 

Now whenever I try to run python I get ImportError: no module named cookielib and urllib2. What happened? Please help me.

Nghĩa Haru
  • 19
  • 1
  • 2

2 Answers2

3

These two libraries are renamed / changed in python 3. this and this should help you out.

mojosama
  • 31
  • 2
0

Check first which python version you are using to execute code. To check python version use check this in terminal:

C:\Users\real>python --version
Python 3.9.10

If you are working with python3.X, then library urllib2, cookielib are not available for any version 3.x.

I found this resources if you wand to migrate from python2.x to python3.x The cookielib module has been renamed to http.cookiejar in Python 3.0. Docs for http.cookiejar.

try:
    import cookielib
except:
    import http.cookiejar
    cookielib = http.cookiejar

Also urllib2 module is changed to urllib3 module in python3.X. Docs for urrlib3