Python

From DreamHost
Jump to: navigation, search

Overview

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).

Deployment

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

Virtualenv creates an environment that 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:

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/example.com/env/bin/activate

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 will be 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

Note2 icon.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 python.org.

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:
    wget http://www.python.org/ftp/python/2.7.7/Python-2.7.7.tgz
    
  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
    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
    
    Note2 icon.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 https://bootstrap.pypa.io/get-pip.py > ~/tmp/get-pip.py
    python ~/tmp/get-pip.py --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

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 ./dreamhost_python_setup.sh --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
wget http://prdownloads.sourceforge.net/swig/swig-3.0.5.tar.gz 
tar xvzf swig-3.0.5.tar.gz 

cd swig-3.0.5 

# configure
# If you're using the Unix account setup, per http://wiki.dreamhost.com/Unix_account_setup, 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
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

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
wget http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py

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: