From DreamHost
Jump to: navigation, search


Python is a programming language like PHP and Ruby which allows you to create many different types of applications. More information can be found on the Python website below:

This article guides you through how to set up your environment at DreamHost to use Python.

Guidelines for setting up a Python file at DreamHost

All Python CGI scripts on DreamHost must:

  • end in ".py" (NOTE: ".cgi, .fcgi" works as well).
  • have #!/usr/bin/python in the very first line of the file (NOTE: #!/usr/bin/python2.x or #!/usr/bin/env python2.x will work as well).
  • be marked as executable: chmod 755.
  • use UNIX style newlines, not Windows (see this Wikipedia article).
  • If you want to view printed output from your Python code, you must print "Content-type: text/html\n\n" as the first line of output.
  • If you don't want .py files to be executed by Apache add "RemoveHandler .py" command to your .htaccess file.
  • Additionally, on DreamHost, all Python CGI scripts AND their immediate parent directory must have their Unix user and Unix group left set to your domain's Unix user AND that user's DEFAULT Unix group (otherwise you will get an internal server error from Apache's suexec).


You have a few options on how to deploy your Python app at DreamHost:

  • CGI – Slow and not recommended.
  • FastCGI – Faster than CGI and uses mod_fcgi.
  • Passenger – The preferred way to deploy Python apps. This is an Apache module which was originally written to support the execution of Ruby on Rails apps, but which also provides support for WSGI-compliant applications. View the Passenger article for setup details.


Virtualenv is a tool used to create an isolated Python environment. This environment has its own installation directories that doesn't share libraries with other virtualenv environments (and optionally doesn't access the globally installed libraries either).

Virtualenv is the easiest and recommended way to configure a custom Python environment.

Installing Virtualenv

You can use pip to install virtualenv. Run the following command in your user's directory:

pip install virtualenv

Once you’ve installed virtualenv, you can cd into your website directory where you’ll set up a new virtualenv:

virtualenv $HOME/<domain>/env

This creates a local copy of your environment specific to this website. While working on this website, you should activate the local environment in order to make sure you're working with the right versions of your tools and packages. Run the following to activate your local environment:

source $HOME/

The name of the current virtual environment will now appear on the left of the prompt. For example, (env)servername:your_project UserName$) lets you know that it’s active. From now on, any package that you install using pip is placed in the env folder, isolated from the global Python installation.

You can then install packages as usual.

If you are done working in the virtual environment for the moment, you can deactivate it:

$ deactivate
  • This puts you back to the system’s default Python interpreter with all its installed libraries.
  • To delete a virtual environment, just delete its folder (in this case, it would be rm -rf env).

Installing custom modules

It’s recommended that you use pip to easily install custom modules. For example:

pip install <module>

For example, (e.g., MySQL-python if you're going to use a MySQL database) for your website using pip:

pip install MySQL-python

View the following link for further details:

Installing a custom version of Python

Dh-kb-note-icon.fw.png Note: As of August 2015, DreamHost currently runs Python v2.7.3.

To install a custom version of Python, you must download it from

For the following commands, replace VERSION with the version of Python you wish to install.

  1. Log into your web server via SSH.
  2. Run the following command to download your desired version of Python:
  3. Decompress the .tar file you downloaded:
    tar zxvf Python-2.7.7.tgz
  4. Change your directory into the Python version folder you just downloaded:
    cd Python-2.7.7
  5. Run the following to install. For example, to install Python 2.7.7:
    ./configure --prefix=$HOME/opt/python-2.7.7
    make install
    • This installs your local version of Python to /home/<username>/opt/python-2.7.7.
  6. Include the path in order to use this version of Python over the system default by adding the following to your .bash_profile file which is located in your user’s directory:
    export PATH=$HOME/opt/python-2.7.7/bin:$PATH
  7. Run the following to update to .bash_profile:
    . ~/.bash_profile
  8. Check which version of Python you're now using:
    python -V
    Dh-kb-note-icon.fw.png Note: In order to install various Python packages, and Django in particular, you'll want pip. This is pre-installed on shared servers, but if necessary, you can use the following to download:
    curl > ~/tmp/ python ~/tmp/ --user

pyenv: simple Python version management

pyenv lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well. Follow the instructions here:

Since the user doesn't have permission to the /var/foo/bar/tmp folder, you will have a "BUILD FAILED" error. Make sure you create a tmp folder in your home folder and tell pyenv to use it:

cd ~
mkdir tmp
echo 'export TMPDIR="$HOME/tmp"' >> ~/.bash_profile

Due to resource limitations on shared hosting, your processes may be killed when compiling math modules such as numpy, pandas, matplotlib, etc. The best solution is to use `anaconda` or `miniconda` packages under `pyenv`.

pyenv update
pyenv install --list # check what's available
pyenv install anaconda-x.x.x # install the version you desire

Once you have `anaconda` as your Python environment, you can use `conda install` to install precompiled math modules, without the hassle of compiling. View the following article for further information:

Automatic installation

A DreamHost user has written a script to automatically download and install a custom Python environment including Python v2.7.3, setuptools, pip, virtualenv. Download the file and then modify the rcfile, version, setuptools_version variables if necessary:

Choose between a '--pythonbrew' or '--source' installation.

bash ./ --pythonbrew

A newer SWIG

SWIG (the Simplified Wrapper and Interface Generator) is used by some packages to easily generate bindings to programming languages (e.g., Python).

These instructions are for installing SWIG version 3.0.5 from source:

mkdir ${HOME}/soft

cd ${HOME}/soft

# download and extract the package
tar xvzf swig-3.0.5.tar.gz 

cd swig-3.0.5 

# configure
# If you're using the Unix account setup, per, configure using:
./configure --prefix=${RUN}

# Otherwise, and assuming you've followed the virtualenv directions above, configure using:
./configure --prefix=$HOME --with-perl5=/usr/local/bin/perl --with-python=$HOME/opt/python-2.7.7/bin/python  --with-ruby=/usr/bin/ruby

# compile and verify
make check

# you might get some errors about ocaml.
# these don't matter (unless you intend to
# use this version of SWIG to generate
# ocaml bindings too).

make install


setuptools is a collection of enhancements to the Python distutils that allow you to more easily build and distribute Python packages, especially ones that have dependencies on other packages.

You can easily install setuptools to work with your version of Python. (Note that virtualenv will set up setuptools automatically.)

mkdir ${HOME}/soft
cd ${HOME}/soft

Now you have the easy_install script installed in your ${RUN}/bin directory.

Web frameworks

Some Python website frameworks can run on DreamHost servers. These include: