Noodlecode parody of spaghetti code

Tag: python

MySQL-python on Windows

Probably you have tried easy_install MySQL-python on Windows but you stuck with this error:

Running -q bdist_egg --dist-dir c:usersusernameappda
error: The system cannot find the file specified

Solution, get the installer:

Trac on Fedora

This is multiple projects installation, a continuation from previous posts.

Install required packages:

yum install trac mod_wsgi

Software: Trac 0.11.7, mod_wsgi 2.3

Trac folder: /var/svn/trac
Python eggs cache dir: /tmp/egg-cache

Create new trac environment

trac-admin /var/svn/trac/testproj initenv
  Project Name > Test Project
  Database connection string > [sqlite:db/trac.db]
  Repository type > [svn]
  Path to repository > /var/svn/repos/testproj

Create wsgi script

vim /var/svn/trac/trac.wsgi
#!/usr/bin/env python
import os
def application(environ, start_request):
  os.environ['TRAC_ENV_PARENT_DIR'] = '/var/svn/trac'
  os.environ['PYTHON_EGG_CACHE'] = '/tmp/egg-cache'
  from trac.web.main import dispatch_request
  return dispatch_request(environ, start_request)

Apache mod_wsgi settings

vim /etc/httpd/conf.d/trac.conf
# comment all settings in /etc/httpd/conf.d/wsgi.conf
LoadModule wsgi_module modules/
WSGIScriptAlias /trac /var/svn/trac/trac.wsgi
<Directory /var/svn/trac>
  WSGIApplicationGroup %{GLOBAL}
  Order deny,allow
  Allow from all
<LocationMatch "/trac/[^/]+/login">
  AuthType Digest
  AuthName "Project Repository"
  AuthUserFile /var/svn/auth
  Require valid-user

Grant administration right to admin user (trac>=0.11)

trac-admin /var/svn/trac/testproj permission add user1 TRAC_ADMIN

Set ownership

chown -R apache.apache /var/svn

Reload apache

service httpd reload

View list of projects – http://localhost/trac

Recursive delete using Python

Let’s say you have a folder with many (and deep) subfolders, and in each folder, you want to delete ‘Thumbs.db’ file:

#!/usr/bin/env python

import os

def delete(root, name):
    os.unlink(os.path.join(root, name))

def main():
    for root,dir,files in os.walk(os.getcwd()):
        for f in files:
            if f.lower().endswith('.db'):
                if f == 'Thumbs.db':
                    print 'Delete %s' % os.path.join(root, f)
                    delete(root, f)

if __name__=='__main__':

Save this script as ‘’. Go to top folder where you want to delete those ‘thumbs’ and execute it:

cd ~/Music
python ~/

Turbogears 2.1 on Fedora

This post shows how to install Turbogears2 (TG2) in a Python virtual environment. (Update: Using Fedora 14)

Install required software:

yum install python-setuptools python-virtualenv

PostgreSQL dependencies (needed by PyGreSQL)

yum install gcc postgresql-devel

For MySQL backend, dependencies:

yum install gcc mysql-devel

Install TG2 in virtual env: (as root)

mkdir -p /usr/local/pythonenv
virtualenv --no-site-packages /usr/local/pythonenv/HelloWorld

Activate virtual env:

source /usr/local/pythonenv/HelloWorld/bin/activate

Install PostgreSQL database components: (in virtualenv, as root)

easy_install PyGreSQL
easy_install psycopg2

Install TG2.1: (in virtualenv, as root)

easy_install -i tg.devtools
easy_install -i Genshi
easy_install tw.jquery

Start developing TG2 app:

Note: Another way to add dependencies – edit, add package name to ‘install_requires’ variable. For MySQL, add ‘MySQL-python’

Activate virtualenv first (as regular user – ‘webmaster’)

source /usr/local/pythonenv/HelloWorld/bin/activate

Create TG2 app:

paster quickstart HelloWorld

Install required packages for this new app: (in virtual env, as root)

cd HelloWorld/
python develop

Make sure to use new PostgreSQL DSN format: (development.ini)

postgresql://username:[email protected]:port/databasename

Populate database (as regular user, refer HelloWorld/helloworld/

paster setup-app development.ini

Launch TG standalone server

paster serve --reload development.ini

… and start coding / debugging at http://localhost:8080

To change default URL address: (development.ini)

host =
port = 5000

Turbo + Gear

Copyright © 2017 Noodlecode