Category: Python


Advnaced Django Tutorial

February 7th, 2012 — 6:36pm

2 years old but worth watching. Good Stuff.

PyCon 2010: Intermediate Tutorial by James Bennett – Django’s release manager, and also contributes to the documentation and provide the occasional bugfix.
Currently, he’s part of the Web development team at Mozilla.

Comment » | Django, Python

Python – Calling Python Function From Terminal

January 26th, 2012 — 7:00pm

#!/usr/local/bin/python
# how to call this
# $ python /Users/nreeves/Desktop/test.py greeting “How are you?”
# >> Hello greeting
# $ python /Users/nreeves/Desktop/test.py food “Rice” “Potato”
# >>I want to eat food and food
# $ python /Users/nreeves/Desktop/test.py foo “bar”
# >>programmer error!

import sys

def greeting(arg1):
    print‘Hello%s% (arg1)

def food(arg1, arg2):
    print‘I want to eat%sand%s% (arg1, arg2)

def main():
    if len(sys.argv) > 1: function=sys.argv[1]
    if len(sys.argv) > 2: arg1=sys.argv[1]
    if len(sys.argv) > 3: arg2=sys.argv[1]

    if function ==“greeting”:
        greeting(arg1)
    elif function ==“food”:
        food(arg1, arg2)
    else:
        print“programmer error!”

if __name__ == “__main__”:
    main()

Comment » | Python

Install Python2.7.2, apache, wsgi on Ubuntu 10.04 LTS

September 27th, 2011 — 7:30pm

My initial attept failed miserably as you can see my post from here

After bunch of help from many intelligent people I successfully setup Python2.7.2, apache, wsgi for Ubunut 10.04 LTS

++++++++++++++++++++++++++++++
+ References:
+ 1. How to install 2.7 comment made by Alen here
+ 2. How to install wsgi
++++

# ==== Setup Ubuntu 10.04 on VMWare # on server:
# install basic stuff…
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install openssh-server # only if you want to use ssh
sudo apt-get install vim # only if you like vim….
sudo apt-get install libpq-dev
sudo apt-get install curl # only if you like over wget.. I am just so used to curl over wget since os-x (my dev machine) doesn’t have wget
sudo apt-get install build-essential

##### # Install Python 2.7.2 #####

sudo vim /etc/apt/sources.list

# add the following 2 lines at the bottom of the file:

deb http://ppa.launchpad.net/fkrull/deadsnakes/ubuntu lucid main
deb-src http://ppa.launchpad.net/fkrull/deadsnakes/ubuntu lucid main

# issue the following command:

sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys DB82666C
sudo apt-get update
sudo apt-get install python2.7 python2.7-dev

# verify instllation

which python >>/usr/local/bin/python
python -V >> Python 2.7.2

# verify the linkage

ldd /usr/local/bin/python2.7

##### # Install Apache #####
# you need to install apache2-dev for wsgi installation

sudo apt-get install apache2 apache2-dev

# then add domain.

sudo vim /etc/apache2/conf.d/fqdn
# add: “ServerName localhost” without quotes

################################
# install wsgi
################################
# if wsgi is installed via apt-get then purge it:

sudo apt-get purge libapache2-mod-wsgi

mkdir tmp # or wherever you want to compile
cd tmp
curl -O http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz tar xzf mod_wsgi-3.3.tar.gz
rm mod_wsgi-3.3.tar.gz
cd mod_wsgi-3.3 ./configure –with-python=/usr/local/bin/python make sudo make install
# the load declaration for the module needs to go wsgi.load
sudo vim /etc/apache2/mods-available/wsgi.load
# then add the following line: “LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so” without quotes
# Then you have to activate the wsgi module with:
sudo a2enmod wsgi

# That’s it!

Comment » | apache, Bash Shell, Django, Linux, mod_wsgi, Python, Ubuntu

Setting up mod_wsgi for Django app on OS X Snow Leopard (10.6.8) for Dev env

July 30th, 2011 — 4:44pm

We are writing Django app right now. Our dev machine is OS X 10.6.8.

Our dev team decided that we should have similar environment as production so I started to set up mod_wsgi on my macbook pro.

There are a few steps that was pain to figure out hence I decided to post hoping to help others.

[APACHE]

Apache is loaded already. It should be no pain unless your home is set up for FileVault and your application directory is located under your home directory. The error I was getting due to FileVault and having my app under home directory was “Forbidden Error”.

This should not be the case for Production environment since we won’t be using OS X nor FileVault. The fix is not recommended but I think it is acceptable on Dev environment. If you know the better solution, comments would be appreciated.

The fix is to change the  User and Group from www to your user name.

User www
Group www

then problem solved. How to find out your user name and group? Issue $ id from the terminal.

Oh and every time you change your httpd.conf, don’t forget to restart apache

$ sudo /usr/sbin/apachectl -k restart

[Apache - enable virtual host]

Just uncomment the line below in httpd.conf

Include /private/etc/apache2/extra/httpd-vhosts.conf

Then specify your DocumentRoot there in httpd-vhosts.conf

Drop index.html under the DocumentRoot you specified. You should see your page as you point browser to http://localhost

[Download and install mod_wsgi module]

# download

$ curl -O http://modwsgi.googlecode.com/files/mod_wsgi-macosx106-ap22py26-3.3.so

# rename

$ mv mod_wsgi-macosx106-ap22py26-3.3.so mod_wsgi.so

# copy

$ sudo cp mod_wsgi.so /usr/libexec/apache2/

Modify tthpd.conf: add the following line as last line of LoadModule

LoadModule wsgi_module     libexec/apache2/mod_wsgi.so

[Install mod_wsgi]

$ curl -O http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz

$ tar xvfz mod_wsgi-3.3.tar.gz

$ rm mod_wsgi-3.3.tar.gz

$ cd mod_wsgi-3.3

# make sure you will  specify your default python interpreter you are using.

$ ./configure –with-python=/Library/Frameworks/Python.framework/Versions/2.7/bin/python

$ sudo make install

if ALL is good then in /private/var/log/apache2/error_log, you should see:

Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8r DAV/2 mod_wsgi/3.3 Python/2.7.1 configured — resuming normal operations

Do some cleanup

$ make clean

add “django.wsgi” to your path_to_your_application/apache/

the script should look like

import os

import sys

path = ‘/Users/user_name/app_name

if path not in sys.path:

sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = ‘settings’

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

def application(environ, start_response):

status = ’200 OK’

output = “Hello World!”

response_headers = [('Content-type', 'text/plain'),

('Content-Length', str(len(output)))]

start_response(status, response_headers)

return [output]

Then add the following line to your httpd-vhosts.conf if you setup for virtual host otherwise httpd.conf

WSGIScriptAlias / /Users/user_name/app_name/apache/django.wsgi

If you point your browser to http://localhost, you should see ”Hello World!”

Now, you want to run your app. Comment out or remove application function from django.wsgi. So it will look like this:

import os

import sys

path = ‘/Users/user_name/app_name

if path not in sys.path:

sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = ‘settings’

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

Now refresh your browser, you should see your Django app home page! (i hope)

Comment » | Django, Python

Back to top