How to fix AttributeError: type object 'CookieJar' has no attribute 'LWPCookieJar'?

2.8k Views Asked by At

I am using cookielib(cookiejar named now in Python3) and mechanize.

I am importing this way

import requests
from bs4 import BeautifulSoup
import mechanize
try:
    from cookielib import Cookie, CookieJar         
except ImportError:
    from http.cookiejar import Cookie, CookieJar as cookielib    

but I keep getting this error (AttributeError: type object 'CookieJar' has no attribute 'LWPCookieJar') at this point of my code

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

Any ideas?

Thanks a lot for your time!

1

There are 1 best solutions below

0
Iain Shelvington On

You can import http.cookiejar as cookielib when using python 3, they have almost the exact same interface.

try:
    import cookielib         
except ImportError:
    from http import cookiejar as cookielib

# Now you can use cookielib.LWPCookieJar no matter what version of python you're using
cookielib.LWPCookieJar()
cookielib.CookieJar()  # or the other classes