29

Dreamhost is a great host for small project. And it's also Django friendly hosting. Everything good except python and Django version is a little bit out of date. Well it's a whole day of work to figure out how to update Python 2.7.3, Django 1.4 on dreamhost and I really want to share with whoever finding it

James
  • 13,571
  • 6
  • 61
  • 83

2 Answers2

48

I currently have private server, a shell account and a bit of luck. So here is what I do:

  1. SSH to your host to upgrade python

     cd ~
     mkdir tmp
     cd tmp
     wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
     tar zxvf Python-2.7.3.tgz
     cd Python-2.7.3
     ./configure --enable-shared --prefix=$HOME/Python27 --enable-unicode=ucs4
     make
     make install
    
  2. Configure system to use our new Python. Open ~/.bashrc and add the following line

     export PATH="$HOME/Python27/bin:$PATH"
     export LD_LIBRARY_PATH=$HOME/Python27/lib
    
     #save it and run
     source ~/.bashrc
    

    you can now check your python version with which python

  3. Install easy_install, pip

    cd ~/tmp
    wget http://peak.telecommunity.com/dist/ez_setup.py
    python ez_setup.py
    easy_install pip
    # Or even shorter
    wget https://bootstrap.pypa.io/get-pip.py
    python get-pip.py
    
  4. Install virtualenv

     pip install virtualenv
     virtualenv $HOME/<site>/env
     #Switch to virtualenv
     source $HOME/<site>/env/bin/activate
    

    you can also add env path to bashrc

     export PATH="$HOME/<site>/env/bin/:$PATH"
     source ~/.bashrc
    
  5. Install django and everything else

     pip install django
     pip install ....
     pip install ....
     pip install ....
    
  6. Create project

     cd $HOME/<site>/
     python $HOME/<site>/env/bin/django-admin.py startproject project
    
  7. Create passenger_wsgi.py in HOME/<site>/ with following content

     import sys, os
     cwd = os.getcwd()
     sys.path.append(cwd)
     sys.path.append(cwd + '/project')  #You must add your project here or 500
    
     #Switch to new python
     #You may try to replace $HOME with your actual path
     if sys.version < "2.7.3": os.execl("$HOME/<site>/env/bin/python",
         "python2.7.3", *sys.argv)
    
     sys.path.insert(0,'$HOME/<site>/env/bin')
     sys.path.insert(0,'$HOME/<site>/env/lib/python2.7/site-packages/django')
     sys.path.insert(0,'$HOME/<site>/env/lib/python2.7/site-packages')
    
     os.environ['DJANGO_SETTINGS_MODULE'] = "project.settings"
     import django.core.handlers.wsgi
     application = django.core.handlers.wsgi.WSGIHandler()
    

or this way

import sys, os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

sys.path.append(os.path.join(BASE_DIR))  #You must add your project here or 500

#Switch to new python
#You may try to replace $HOME with your actual path
PYTHON_PATH = os.path.join(BASE_DIR, 'env', 'bin', 'python')
if sys.executable != PYTHON_PATH:
    os.execl(PYTHON_PATH, "python2.7.12", *sys.argv)

If you are using django 1.7, replace the last two line with

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
  1. Enjoy :D

New version of python on Dreamhost will no longer return sys.executable so you this is my version of passenger_wsgi

import sys, os

VIRTUAL_ENV_PYTHON = 'venv-python'  # Python > 2.7.6 dreamhost not return sys.executable
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

def is_venv_python():
    if len(sys.argv) > 0:
        last_item = sys.argv[len(sys.argv)-1]
        if last_item == VIRTUAL_ENV_PYTHON:
            return True
    return False

sys.path.append(os.path.join(BASE_DIR))  #You must add your project here or 500

#Switch to new python

PYTHON_PATH = os.path.join(BASE_DIR, 'env', 'bin', 'python')
if not is_venv_python():
    os.execl(PYTHON_PATH, "python2.7.12", *sys.argv + [VIRTUAL_ENV_PYTHON])

sys.path.insert(0, os.path.join(BASE_DIR, 'env', 'bin'))
sys.path.insert(0, os.path.join(
    BASE_DIR, 'env', 'lib', 'python2.7', 'site-packages'
))
James
  • 13,571
  • 6
  • 61
  • 83
  • 4
    It would be great if someone can write an automation script – James Jun 08 '12 at 17:55
  • we did not create any project directory inside env, did we? sys.path.insert(0,'$HOME//env/project') getting An error occurred importing your passenger_wsgi.py even after /project from the that) – Anuvrat Parashar Jun 13 '12 at 21:28
  • A slightly more general version of your python installation process is available in this [gist](https://gist.github.com/4699533) – hobs Feb 02 '13 at 22:30
  • And to complete the generalization exercise, here's the [gist](https://gist.github.com/4699850) of the other half to install pip, venv, and a django app – hobs Feb 03 '13 at 00:10
  • 1
    For me I had an issue when it came to installing pip. When I ran `easy_install pip` it wanted to install it to the python 2.6 path. I ended up having to run `easy_install-2.7 pip` – Bot May 20 '13 at 21:39
  • You have to call `source your_enviroment_script` to change enviroment to your new path.... everytime you use shell – James May 21 '13 at 02:09
  • 1
    You should comment out instructions like "Open ~/.bashrc and add the following line" in step 2 so people don't run into issues when they copy and paste the code samples. – Zach Jun 27 '13 at 00:09
  • Also why not source .bashrc from .bash_profile so that it defaults to Python 2.7 upon login? – Zach Jun 27 '13 at 02:26
  • 1
    I needed to do `python ez_setup.py prefix=$HOME/Python27 -U setuptools`, followed by `easy_install-2.7 pip` – Hugh Perkins Jul 01 '13 at 13:34
  • I get error: An error occurred importing your passenger_wsgi.py what it could be? I changed every 'project' and to my project and mysite. – user1406647 Mar 05 '14 at 09:59
  • "An error occurred importing your passenger_wsgi.py" normally this is caused by syntax error in your passenger.py file, or missing import statement. Where did you get the error, you can check apache log for more information – James Mar 05 '14 at 10:09
  • I tried 'python passenger_wsgi.py' in command prompt and it didnt turned any error. also my browser is very slow when i type address. and i get this error. (i dont get any record in error log, just in access log) – user1406647 Mar 05 '14 at 12:59
  • What happend when you try to start server using shell: python manage.py runserver ? – James Mar 06 '14 at 06:44
  • 1
    Amazing!! I struggle with the installations for the whole weekend. Thanks – neoprez Jul 06 '15 at 07:57
1

Currently Dreamhost updated servers to Ubuntu 12.04, and i've got an error:

Import Error: <path-to-python>/_io.so undefined symbol: PyUnicodeUCS2_Decode

after compiling custom python and running "python ez_setup.py"

The solution was to compile python with --enable-unicode=ucs4 at step 1

./configure --enable-shared --prefix=$HOME/Python27 --enable-unicode=ucs4
Max Elahov
  • 221
  • 4
  • 9
  • 1
    *dont know* It also should be said, that current default python version on "happy hosting" == 2.7.3, but Django is still old, so I use your steps for creating virtualenv. It works fine! Thank you! – Max Elahov Oct 13 '14 at 19:41