Installing Python 3.4 and setting up a virtualenv

As an update to my snippet on installing Python 3, the installation and setup of virtualenvs with Python 3.4 is a little different, namely because python 3.4 ships with its own virtual environment manager, Pyvenv.

1. Download the tarball and extract

$ cd /tmp
$ wget https://www.python.org/ftp/python/3.4.0/Python-3.4.0.tgz
$ tar -xvf Python-3.4.0.tgz

2. Run configure using the –prefix flag to specify installation location

$ ./configure --prefix=/usr/bin/python3.4

3. Make and install

$ make && sudo make install

4. Use the `pyvenv` to make a virtualenv, supplying the location of your virtualenvs

$ /usr/bin/python3.4/bin/pyvenv3.4 ~/.envs/py34

5. Activate your virtualenv

$ . ~/.envs/py34/bin/activate

6. Check Python version

$ python -V
Python 3.4.0

You might want to also make a bash alias to facilitate the creation of virtual environments this way.

UPDATE

I have noticed that often when compiling Python 3.X from source it complains about not being able to find the Bz2 module.

Some very good instructions for solving this are here: http://justanyone.blogspot.ca/2012/07/compiling-python-with-bz2.html

Basically you just need to make Bz2 prior to Python.

Advertisements

Getting data into a Django application on Heroku

You can use Django to dump data from your db and git to push the dump to heroku. If the data is sensitive you should take care to remove the data by resetting the head after loading the data in heroku. A more in depth solution can be found here.

1. Dumpdata from dev build.

python manage.py dumpdata --natural --indent 2 > data.json

2. Add data to repo and commit.

git add data.json

git commit -m "Dump data"

3. Push to Heroku.

git push heroku master

4. Load data into Heroku build.

heroku run python manage.py loaddata data.json

Setting up Postgres for Django on Debian Linux

Assumptions: Debian-based linux distro

1. Install postgres

sudo apt-get install postgresql

2. Create a password for the default postgres user

sudo passwd postgres

3. Login as PostGres user

su - postgres

4. Connect to postgres

psql

5. Start Database as root (automatically runs initdb)

service postgresql start

6. Create a role (the Django user in the DATABASES dictionary in settings.py is actually a role)

CREATE ROLE somebody WITH LOGIN PASSWORD 'secret123';

7. Create  a database

create database mydatabase

8. Now configure settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydatabase',
        'USER': 'somebody',
        'PASSWORD': 'secret123',
        'HOST': ''  #localhost
    }
}

Done!

Set Up New Relic for a Django project

1. Setup a New Relic account:

http://newrelic.com/signup

2. Install the newrelic app

pip install newrelic

2. Copy your license key

4. Create the new relic config file:

newrelic-admin generate-config LICENSE-KEY newrelic.ini

5. Edit the newrelic.ini file to give your application a name:

app_name = My Python Application

6. Update wsgi.py:

import newrelic.agent
import os

newrelic.agent.initialize('/home/username/path/to/myproject/newrelic.ini')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") 

from django.core.wsgi import get_wsgi_application 

application = get_wsgi_application()
application = newrelic.agent.wsgi_application()(application)

6. Verify it’s working:

newrelic-admin validate-config newrelic.ini

7. Start making request to the dev server and log into your New Relic account to see the data!

Set up git with ssh keys on bitbucket

I found the bitbucket documentation on this a little verbose.

So here is my minimalist guide. I assume that there is already a repo created in bitbucket.

1.  Copy the ssh url from bitbucket. On your repo page there should be a section along the lines of Clone this repository.

Hit SSH and copy the url part (e.g. git@bitbucket.org:myproject/myproject.git).

2.  On the local machine:

    $ git remote set-url bitbucket

3.  Check the remote address:

    $ git remote -v

This should show something like:

    origin git@bitbucket.org:myproject/myproject.git (fetch) 
    origin git@bitbucket.org:myproject/myproject.git (push)

4.  Now let’s generate a new SSH key. If you already have an SSH key for the machine, then that can be used. These instructions assume this is the first. Go ahead an accept the default location for saving and if you don’t want to be asked for a password each time you push/pull (which is one of the big advantages of using ssh keys), then hit Enter for the passphrase.

    $ ssh-keygen

5.  Go to the public part of the saved key. It’s probably in /home/.ssh/id_rsa.pub. Copy it.

6.  Now, on the bitbucket site, go to user->account->ssh keys, and paste the key into the box. Label the key something logical.

7. Back on the local machine:

    $ git push origin master

This should push your repo as normal, but using the SSH protocol and without the need for a password.

End.

Git remove all .pyc files

I ran into this recently with a project which already had several git commits but was still tracking .pyc files. This was starting to cause problems so I had to find a way to remove them from the repo:

    $ find . -name "*.pyc" -exec git rm -f {} \;

Then *.pyc can be added to the .gitignore file.

Thanks to Yuji Tomita for this information which can be found here.

Using GDAL with GeoDjango

So, just a quick solution to a simple but possibly common problem. When running through the official GeoDjango tutorial I had some problems with the GDAL library being recognized.

I did this:

    $ ogrinfo world/data/TM_WORLD_BORDERS-0.3.shp

and go this:

    $ ogrinfo world/data/TM_WORLD_BORDERS-0.3.shp

ogrinfo: error while loading shared libraries: libgdal.so.1: cannot open shared object file: No such file or directory

The answer, which took me a little while to find, was to add the library path, /usr/local/lib to /etc/ld.so.conf.

Then activate the path:

    $ ldconfig

Now all is fine.