15

I have looked everywhere for this and just cannot find the answer. I have checked my python version and it is version 3.2 . When I try to import cookielib I receive:

ImportError: No module named cookielib

I have seen that in Python 3.0 it was renamed to http.cookiejar and that it would auto import cookielib.

I thought that maybe there was some wild error in my python configuration so I thought I should try and import http.cookiejar like this import http.cookiejar. That did not work all and I get and error:

EOFError: EOF read where not expected.

This is not the error I had expected becuase import http.cookies imports just fine.

Does anybody have a solution to this problem? What am I overlooking?

Full Error:

Traceback (most recent call last):
  File "C:\Users\Spencer\Downloads\selenium-2.20.0.tar\selenium-2.20.0\selenium-2.20.0\test", line 1, in <module>
    import urllib.request, urllib.parse, http.cookiejar
EOFError: EOF read where not expected
phihag
  • 278,196
  • 72
  • 453
  • 469
Sneitzke38
  • 427
  • 2
  • 5
  • 12

4 Answers4

11

The automatic renaming business only applies if you use 2to3. Therefore, you have to import http.cookiejar.

The error EOFError: EOF read where not expected is only ever thrown by Python marshalling. Most likely, this is caused by a race condition fixed in Python 3.3, where multiple processes tried to write concurrently to the pyc file. Deleting all .pyc files may be a workaround.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • For those who come across this in the future: reinstalling Python seems like a drastic step. Just deleting the `.pyc` files from whatever library is causing the `EOFError` was sufficient for me just now. – Danica May 26 '12 at 22:57
  • 1
    The EOFError: is most likely a race condition when 3 or more Python processes read/write the same pyc file. Fixed in Python 3.3: http://bugs.python.org/issue13146 – Guido van Rossum May 07 '16 at 02:21
  • @GuidovanRossum Thanks for the pointer! I've updated the answer. – phihag May 07 '16 at 08:13
8
try:
    import cookielib
except:
    import http.cookiejar
    cookielib = http.cookiejar
double-beep
  • 5,031
  • 17
  • 33
  • 41
Joel Silva
  • 81
  • 1
  • 1
4

The cookielib module has been renamed to http.cookiejar in Python 3.0. The 2to3 tool will automatically adapt imports when converting your sources to 3.0.

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
Roozbeh
  • 523
  • 4
  • 11
0

My initial guess is that you have a corrupted library file. Inside your Python installation, look at lib/python3.2/http/cookiejar.py and scroll down to the end. Mine (Python 3.2.2) ends in the save() method definition with

finally:
    f.close()

If you see anything else, your installation is probably broken and I'd recommend reinstalling it.

Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65