0

From what I've learned from my research here and elsewhere, it seems that if a) a module is located in the Python search path or b) contained in a package that is in the Python search path, that the import command should be able to find and import the module.

In the interactive script below, note that both of these conditions have been satisfied. the http folder contains an __init__.py file, making it a package, and that folder contains a module named cookies.py. Yet, the command import http.cookies fails, and the traceback looks like Python is searching for this module in the django folders, which I know will fail as there is no cookies.py module in the django http package folder. I also tried to manipulate the search path by editing my PYTHONPATH to ensure that the Standard Library http package folder is earlier in the search path, but as you can see below, the import is still failing.

What is causing this failure?

Python 3.3.1 (v3.3.1:d9893d13c628, Apr  6 2013, 20:25:12) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import sys
>>> for item in sys.path: print(item)


C:\Python33\Lib\idlelib
C:\Python33\lib\site-packages\setuptools-1.1.7-py3.3.egg
c:\Python33\Lib\http
C:\Python33\Lib\site-packages
C:\Python33\Lib\site-packages\django
C:\Python33\Lib\site-packages\django\bin
C:\Windows\system32\python33.zip
C:\Python33\DLLs
C:\Python33\lib
C:\Python33
C:\Python33\lib\site-packages\win32
C:\Python33\lib\site-packages\win32\lib
C:\Python33\lib\site-packages\Pythonwin
>>> import os
>>> os.path.isfile('C:/Python33/Lib/http/__init__.py')
True
>>> os.path.isfile('C:/Python33/Lib/http/cookies.py')
True
>>> import http.cookies
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    import http.cookies
  File "C:\Python33\Lib\site-packages\django\http\__init__.py", line 1, in <module>
    from django.http.cookie import SimpleCookie, parse_cookie
  File "C:\Python33\Lib\site-packages\django\http\__init__.py", line 1, in <module>
    from django.http.cookie import SimpleCookie, parse_cookie
  File "C:\Python33\Lib\site-packages\django\http\cookie.py", line 5, in <module>
    from django.utils.six.moves import http_cookies
  File "C:\Python33\Lib\site-packages\django\utils\six.py", line 86, in __get__
    result = self._resolve()
  File "C:\Python33\Lib\site-packages\django\utils\six.py", line 105, in _resolve
    return _import_module(self.mod)
  File "C:\Python33\Lib\site-packages\django\utils\six.py", line 76, in _import_module
    __import__(name)
ImportError: No module named 'http.cookies'
>>> 
Steve Sawyer
  • 1,785
  • 4
  • 18
  • 21

1 Answers1

2

You added the Django top-level package to your sys.path:

C:\Python33\Lib\site-packages\django

Remove that entry, and don't add top-level packages to your path. Python finds the http top-level package in that directory first, so you are now effectively importing the django.http package, which does not have a cookies module.

You should only add the parent directory of a package to your path. C:\Python33\Lib\site-packages and C:\Python33\Lib are both already listed, so you do not need any of the following:

c:\Python33\Lib\http
C:\Python33\Lib\site-packages\django
C:\Python33\Lib\site-packages\django\bin
C:\Python33\lib\site-packages\win32
C:\Python33\lib\site-packages\win32\lib
C:\Python33\lib\site-packages\Pythonwin
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Excellent! The fact that the _parent directory_ of the packages needs to be in the search path, **not** the package directory somehow escaped me completely. Thanks @Martijn!! – Steve Sawyer Nov 19 '13 at 19:01