2

I want to learn the skill to log in websites with Python, so I copy other's code then try to understand them. I've write several web crawls which don't need to post data to the websites by myself. But this time, when I try to learn crawls that need post data, Python doesn't work.

It's the wrong message from Windows Command Line:

C:\Python26\lib\cookielib.py:1579: UserWarning: cookielib bug!
Traceback (most recent call last):
    File "C:\Python26\lib\cookielib.py", line 1577, in make_cookies
      parse_ns_headers(ns_hdrs), request)
    File "C:\Python26\lib\cookielib.py", line 1534, in _cookies_from_attrs_set
      cookie = self._cookie_from_cookie_tuple(tup, request)
    File "C:\Python26\lib\cookielib.py", line 1527, in _cookie_from_cookie_tuple
      rest)
    File "C:\Python26\lib\cookielib.py", line 764, in __init__
      self._rest = copy.copy(rest)
AttributeError: 'module' object has no attribute 'copy'

    _warn_unhandled_exception()

Actually, I have tried to use Python2.7.6 and Python2.6 and refresh 2.7.6 for times.

The code that I copied is from http://www.oschina.net/code/snippet_1253232_26503. This code is right because lots of people run this code .

I provide an account: Email='13018015260'(Also can be telephone number in this site),password='qwerty'

The code is not the problem, but the Python itself. Once I use cookielib in code, Python on my computer doesn't work!

Sorry for my poor English, I want to know the answer, because I even can't find similar problems in Google!

The screen picture

BenMorel
  • 34,448
  • 50
  • 182
  • 322
hugleecool
  • 205
  • 2
  • 9

1 Answers1

2

Make sure that you do not have your own copy.py or copy.pyc in python path.

You can check that using following command in python interactive shell.

python -c "import copy; print(copy.__file__)"

It should print path something like:

c:\python27\lib\copy.pyc

If the above command print something that does not reference the file in standard library path, renamed the file (copy.py), and make sure you delete copy.pyc file.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Yes, it exits. Yesterday I thought that maybe my Python Files is damaged, so I repair Python 2.7.6 use Python 2.7.6 Windows X86-64 Installer in python.org. It still didn't work. Then I thought maybe is the bug Python itself, I change to python 2.6, it doen't work also. – hugleecool Jan 08 '14 at 06:27
  • C:\Users\hugleecool\python>python -c "import copy; print(copy.__file__)" copy.py – hugleecool Jan 08 '14 at 06:28
  • @hugleecool, Do you mean the command prints `copy.py` ? Then rename the file not to shadow builtin `copy` module. (Remove `copy.pyc` if it exists). Otherwise, it prevent importing of built-in `copy` module. – falsetru Jan 08 '14 at 06:31
  • I removed copy.pyc ,and this time:C:\Users\hugleecool\python>python -c "import copy; print(copy.__file__)" copy.pyc – hugleecool Jan 08 '14 at 06:36
  • @hugleecool, The output tells that there is still `copy.pyc`. ;) – falsetru Jan 08 '14 at 06:37
  • You mean that I should remove copy.py and copy.pyc in the lib file?? – hugleecool Jan 08 '14 at 06:39
  • @hugleecool, No I don't mean it. I means you should remove `copy.pyc` file in `C:\Users\hugleecool\python` directory. And rename `C:\Users\hugleechool\python\copy.py` to other name. – falsetru Jan 08 '14 at 06:40
  • I renmaed copy.py,then:C:\Users\hugleecool>python -c "import copy; print(copy.__file__)" Traceback (most recent call last): File "", line 1, in ImportError: No module named copy – hugleecool Jan 08 '14 at 06:45
  • @hugleecool, Did you removed/renamed `c:\python27\lib\copy.py` ? I didn't mean it. :( – falsetru Jan 08 '14 at 06:47
  • er...When I just removed copy.pyc in lib file, Windows Command Line print:copy.pyc,when I removed copy.pyc and renamed copy.py,WCL says:ImportError: No module named copy – hugleecool Jan 08 '14 at 06:54
  • SORRY, when I removed copy.pyc and then restart WCLine and input'python -c "import copy; print(copy.__file__)"', it becomes to 'C:\Python27\lib\copy.py' – hugleecool Jan 08 '14 at 07:01
  • @hugleecool, Yeah, it should print `C:\python27\lib\copy.py`. Now cookielib bug gone? – falsetru Jan 08 '14 at 07:04
  • No,it still exits. I find that When I run the command 'python -c "import copy; print(copy.__file__)"', it will automatically creates the copy.pyc in lib file – hugleecool Jan 08 '14 at 07:08
  • And I run python -c "import copy; print(copy.__file__)" again, WCLine prints copy.pyc again. – hugleecool Jan 08 '14 at 07:09
  • @hugleecool, `lib` folder ? Do you mean `C:\python27\lib\copy.pyc` ? It's okay. `pyc` file is created if it is imported. – falsetru Jan 08 '14 at 07:12
  • I add an picture in this question. – hugleecool Jan 08 '14 at 07:13
  • @hugleecool, Insert `print copy.__file__` in 764 line (before `self._rest = copy.copy(rest)`) of `c:\python27\lib\cookielib.py`, and let me know what happend. – falsetru Jan 08 '14 at 07:23
  • Your answer inspired me very much! When I copy the code to another folder, it will work!!!! Maybe the code folder I used have some codes that conflict with Python!! Since I have write hundreds codes from September 2013. – hugleecool Jan 08 '14 at 07:26
  • @huglecool: You don't have all your scripts in one folder, don't you? – Matthias Jan 08 '14 at 09:07
  • @Matthias: Actually, yes! I use Notepad++ to write scripts and use Windows Command Line to run them. It's easier to run scripts which are in the same folder. Is there any way more scientific to do? – hugleecool Jan 11 '14 at 06:55