Python/Virtual Env + Pylons + SQLAlchemy
Contents
Abstract
These are instructions on how to get up and running with a new Pylons project.
Install
You'll need to install virtualenv. If you're using Fedora, it's in the 'python-virtualenv' package.
You'll need to install git. In Fedora, the package is simply 'git'.
You'll need PostgreSQL, as well as the -devel packages, so you can build psycopg2 later. That's the postgresql-devel and postgresql-server packages in Fedora.
Run the following command in Fedora to get all of those packages:
$ su -c "yum -y install python-virtualenv git postgresql-devel postgresql-server"
Setup Project Directory
Next you'll need to setup your project directory. Do that using virtualenv.
$ virtualenv --no-site-packages ~/Projects/tmp
This will setup a new virtualenv in that directory. You'll need to activate the environment with the following commands.
$ cd ~/Projects/tmp $ . bin/activate
Initialize Git
Initialize git for the project.
$ cd ~/Projects/tmp $ git init
You want a good .gitignore package.
$ cat >> .gitignore *.pyc *.pyo *.swp *.swo <CTRL-d> to end $ git add .gitignore
You'll want to add in the initial setup.
$ git add * $ git commit -m "Initial setup"
That's it.
Add software with easy_install
You can now add the software you need.
$ easy_install Pylons SQLAlchemy psycopg2
Add these to git as well.
$ git add lib/ $ git commit -m "Added Pylons, SQLAlchemy and psycopg2"
Setup your package
Now it's time to setup your package. When I do this, I just reuse the same directory as the virtualenv. I find it's a bit easier.
$ paster create -t pylons -o ~/Projects/ tmp
Be sure to say "True" when you are asked about adding SQLAlchemy support.
Add in the files created to git.
$ git add * $ git commit -m "Created project."
Create PostgreSQL Database
Now you'll need a PostgreSQL database.
If you haven't already, init PostgreSQL.
$ su -c "service postgresql init"
Then start it:
$ su -c "service postgresql start"
If you want it to start whenever you reboot:
$ su -c "chkconfig postgresql on"
You need to create a user account for yourself.
$ su -c "su -c createuser postgres"
You'll want to user your username as the role to add for simplicity. Give yourself permission to create more databases as well.
Now create your database.
$ createdb tmp-dev
The connection string in your development.ini file is going to look like:
sqlalchemy.url = postgres:///tmp-dev
(Don't forget to commit that to git.)
Since you'll be running as yourself, check that you can access the database:
$ psql tmp-dev
If you can't get in, you'll have to figure out why and fix it. It's likely you messed with the default configuration of PostgreSQL.
After you specify your tables in tmp/model/__init__.py, you can create them with:
$ paster setup-app development.ini
What you Have
The bin/, lib/, and include/ directories are setup by virtualenv. These contain the necessary stuff to get Python working with the modules you installed with easy_install. Feel free to mess with stuff if you really need to. I try to allow easy_install to manage everything.
The data/, <project>/, <project>.egg-info/ and docs/ directories contain the stuff you are building. You'll make the most changes to <project>/ directory.
When you get ready to deploy to production, you can use easy_install in your production environment to install your application code. Or you can use git to copy exactly what you have over.