Installing OpenCV python

Tested on Ubuntu 12.04.4 Desktop (32-bit)

See instructions at

I created a installation script

but found a better more successfull shell one

updated 2014-08-19

Global Modules for Python

When importing inside a python script it first looks to see if the file is inside the current directory but if not it looks to see its in the python global folder(source) which in:


I like to create a symbolic links so I can still have my files inside a directory of my choosing and it being global.

$ ln -s /var/fsq/python/fsq_modules/ /usr/lib/python3.2/fsq_modules

there must be a in the directory for it to work

Python script in the browser

This site shows you how to setup apache2 so you can run python script in the browser (lampp). He shows a method on how to use an html form to fetch date from python using POST.

Running a script directly without POST and using urllib2 to fetch a website:
in the browser type:

import cgi
import cgitb
import urllib2

# Begin HTML generation, must be here
print "Content-Type: text/html; charset=UTF-8" # Print headers
print "" # Signal end of headers

# HTML is pushed to the page with print
# To save time you can use ''' to open and close a print
# statement for block printing

response = urllib2.urlopen('')

print "hello world"

run python script html apache2 server
run python script html
urllib2 execute html

Create executable ubuntu icons for python script

First create you python script

#!/usr/bin/env python3

import sys
from PyQt4 import QtGui, QtCore
class HelloQt(QtGui.QDialog):
    def __init__(self):
        self.setWindowTitle("Hello Qt")
        # Vertical layout
        self.layout = QtGui.QVBoxLayout(self)
        # Create widgets
        self.label = QtGui.QLabel("What's your name?") = QtGui.QLineEdit()
        self.output = QtGui.QLineEdit()
        # Add widgets to the layout
        # Connect with self.sayHello(name) when text is changed
        self.connect(, QtCore.SIGNAL("textChanged(const QString&)"), self.sayHello)
    def sayHello(self, name):
        # Set the output text
        self.output.setText("Hello " + name + "!")
if __name__ == "__main__":
        # Create QApplication
        app = QtGui.QApplication(sys.argv)
        # Create a HelloQt window and show it
        helloqt = HelloQt()
        # Run the mainloop

make sure that your python script has the shebang on the first line.
Make an executable file by running
$ chmod +x

Now create the file /usr/share/applications/runPythonIcon.desktop:

[Desktop Entry]
Name=python gui

make this file also executable by running
$ chmod +x runPythonIcon.desktop



Communication between Arduino and Computer using Python

Python code:

#communications between arduino and computer using python
import serial

#enter your device file
ser = serial.Serial('/dev/<your_device_file>', 9600) //windows: '/dev/com4' | linux: '/dev/ttyACM0'
while 1:
	print ser.readline().strip('\n')

Arduino Code:

 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff


int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
  irrecv.enableIRIn(); // Start the receiver

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value

Additional information

Python Barebones Web Server

page 56 from Programming python O'Reilly 4th edition
Implement an HTTP web server in Python that knows how to run server-side
CGI scripts coded in Python; serves files and scripts from current working
dir; Python scripts must be stored in webdir\cgi-bin or webdir\htbin;
import os, sys
from http.server import HTTPServer, CGIHTTPRequestHandler

webdir = '.' # where your html files and cgi-bin script directory live
port = 80 # default http://localhost/, else use http://localhost:xxxx/

os.chdir(webdir) # run in HTML root dir
srvraddr = ("", port) # my hostname, portnumber
srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)
srvrobj.serve_forever()# run as perpetual daemon

mini small

Python and MySQL

I needed a better way to prevent spammers from coming to my site.  I was manually retrieving their IP address and blocking on my .htaccess file.  However, I decided to automate it by using a python script, crontab, wordpress and MySQL.  My biggest issue was to get python to connect to mysql.

I build a script that fetches every IP address marked as spam in wordpress. Crontab runs this script every week which creates and replaces the .htaccess file with updated data.

htaccess python script, Clean URL

I enjoy the clean URL when it looks like

However every time I create a new app I have to re-edit/create a htaccess file. This is too much work and remembering. Below is a python script for windows and linux that once ran inside the root folder of your site it will create an htaccess file to make that clean URL you always wanted. You can also fork it at GitHub

Warning! You may need to configure apache and php for the htaccess file to work. Click me for instructions.


# this script customizes and creates the .htaccess to correspond to the current directory

import os

def create_htaccess():
f = open(‘./.htaccess’,’w’)
# get directory path
directory = os.getcwd()

# if linux
if == ‘posix’:
dirname = directory[directory.rfind(‘/’)+1:]
# if windows
elif == ‘nt’:
dirname = directory[directory.rfind(“\\”)+1:]
exit(‘error occurred: couldnt determine the system this computer is on’)

# make sure you change the root url on line 27 RewriteRule ^(.*)$ directory/index.php?/$1 [L]
RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#’system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#When your application folder isn’t in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename ‘application’ to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ”’+ dirname + ”’/index.php?/$1 [L]
# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php


if __name__ == ‘__main__’: