3

I am trying to setup django with python3 on dreamhost.

I have setup a virtualenv as documented by them and installed all the pre-requisites. I have gotten runserver working

The problem comes with passenger setup. The error log shows that I am unable to import a module named Cookie, resulting out of further errors.

This is the traceback:

File "/home/user/path/env/lib/python3.4/imp.py", line 171, in load_source
    module = methods.load()
  File "<frozen importlib._bootstrap>", line 1220, in load
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "passenger_wsgi.py", line 17, in <module>
    from django.core.wsgi import get_wsgi_application
  File "/home/user/path/env/lib/python3.4/site-packages/django/core/wsgi.py", line 2, in <module>
    from django.core.handlers.wsgi import WSGIHandler
  File "/home/user/path/env/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 11, in <module>
    from django import http
  File "/home/user/path/env/lib/python3.4/site-packages/django/http/__init__.py", line 1, in <module>
    from django.http.cookie import SimpleCookie, parse_cookie
  File "/home/user/path/env/lib/python3.4/site-packages/django/http/cookie.py", line 5, in <module>
    from django.utils.six.moves import http_cookies
  File "/home/user/path/env/lib/python3.4/site-packages/django/utils/six.py", line 90, in __get__
    result = self._resolve()
  File "/home/user/path/env/lib/python3.4/site-packages/django/utils/six.py", line 113, in _resolve
    return _import_module(self.mod)
  File "/home/user/path/env/lib/python3.4/site-packages/django/utils/six.py", line 80, in _import_module
    __import__(name)
  File "/home/user/path/env/lib/python3.4/site-packages/django/http/__init__.py", line 1, in <module>
    from django.http.cookie import SimpleCookie, parse_cookie
ImportError: cannot import name 'SimpleCookie'

as you can see the line,

from django.core.wsgi import get_wsgi_application

is failing

on the other hand, when i try it with the python interpreter, it imports correctly.

I have also verified that the same interpreter is being used by passenger by logging the variable

 import sys
 raise Exception(sys.executable)

Any ideas as to the cause of this? I tried logging path, it displays the following

['/home/user/path/env/lib/python3.4/site-packages', '/home/user/path/env/lib/python3.4/site-packages/django', '/home/user/path/env/bin', '/home/user/path', '/usr/local/dh/passenger/helper-scripts', '/home/user/path/env/lib/python34.zip', '/home/user/path/env/lib/python3.4', '/home/user/path/env/lib/python3.4/plat-linux', '/home/user/path/env/lib/python3.4/lib-dynload', '/home/user/opt/python-3.4.2/lib/python3.4', '/home/user/opt/python-3.4.2/lib/python3.4/plat-linux', '/home/user/path/env/lib/python3.4/site-packages', '/home/user/path', '/home/user/path/git/package']

which seems fine to me

acid_crucifix
  • 362
  • 2
  • 12
  • Probably the path is borked. Try displaying `sys.path` – Wayne Werner Feb 23 '15 at 19:34
  • @WayneWerner see edits – acid_crucifix Feb 23 '15 at 19:42
  • From your interpreter you can do `from django.http.cookie import SimpleCookie`, right? What about when you put that in file after `import sys`? – Wayne Werner Feb 23 '15 at 20:04
  • yes, you are correct, I can do it from interpreter, but when i do it in my passenger_wsgi.py file, it throws the same error as above. I have verified that it is using the same interpreter as well.. so i am confused as to the problem. I am left to believe that it has something to do with passenger.. which is not a very progressive belief system. – acid_crucifix Feb 23 '15 at 20:33

2 Answers2

1

The instructions in dreamhost wiki works fine. You just need to remove the extra django path in your wsgi config file. It should look like this:

...
cwd = os.getcwd()
sys.path.append(cwd)
sys.path.append(cwd + '/projectname')  #You must add your project here

sys.path.insert(0,cwd+'/env/bin')
sys.path.insert(0,cwd+'/env/lib/python2.7/site-packages')
...

not like this:

...
cwd = os.getcwd()
sys.path.append(cwd)
sys.path.append(cwd + '/projectname')  #You must add your project here
sys.path.insert(0,cwd+'/env/lib/python2.7/site-packages/django')
sys.path.insert(0,cwd+'/env/bin')
sys.path.insert(0,cwd+'/env/lib/python2.7/site-packages')
...
cgl
  • 1,106
  • 1
  • 13
  • 27
0

I ran into a same problem and the following solution works for me. I have Django 1.7 with Python 3.4, using Eclipse with PyDev as my IDE.

My solution is related to this thread: Import Python module fails (http.cookies)

What I did:

  1. In Eclipse, go to tab Project/Properties
  2. Select side-tab PyDev - PYTHONPATH
  3. Select External Libraries
  4. There should be a path which looks like /.../lib/pythonX.Y/site-packages/django. Remove it. (I was using the venv package to create my virtual environment. Your path may differ.)

  5. Run the project and see if it works.

Comment: I think this is indeed a very weird problem... Something to do with the PyDev - DjangoProject setup with Python3, possibly. Let me know if this helps.

Community
  • 1
  • 1
lpounng
  • 570
  • 6
  • 27