Installing Cryptography via Pip with MacPorts or Homebrew

If you have tried installing / updating pyOpenSSL to version 0.14 you may have run into an issue when it tries to install the required package cryptography. The specific error I encountered was:

To get around this, you’ll need to set some environment variables before installing cryptography:

A pull request has been opened to add this to the documentation but as of this writing it has not yet been merged.

PyCharm Command Not Found When Using Virtualenv & MacPorts

I recently switched a project over to using virtualenv for my PyCharm Django server run/debug configuration and ran into a ‘command not found’ error when trying to use another MacPorts installed package (sass) from Django (via Django Compressor).

Here is my original server configuration in PyCharm:

A quick look at the PATH environment variable identified the trouble:

MacPorts Python (/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7)

>>> print os.environ['PATH']

Virtualenv Python (/Users/chriskief/.virtualenvs/exampleproject/bin/python)

>>> print os.environ['PATH']

The MacPorts bin and sbin are missing from the PATH, and that’s where sass lives:

which sass

The solution is to add the MacPorts directories (/opt/local/bin:/opt/local/sbin:$PATH) to your environment variables’ PATH in the run/debug configuration:

As a side note, if you run the Django server using the virtualenv from your terminal, you won’t encounter this error because MacPorts includes these directories in your .profile PATH:

# MacPorts Installer addition on 2013-10-26_at_16:30:30: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.

MacPorts, Mavericks & MySQL 5.6 with Memcached

If you’ve upgraded to Mavericks you’ve probably realized that MacPorts MySQL 5.6 would not build due to some issues with MySQL itself.

That issue has now been fixed with MySQL version 5.6.15 and this changeset which is now live in the port index. Simply install like usual:

sudo port install mysql56-server

One thing the portfile doesn’t contain is the flag to enable the new 5.6 InnoDB Memcached Plugin. If you’d like to enable it, you’ll need to create a local portfile with the following changes:

# change
name                mysql56
# to
name                mysql56-custom

# change
# to

If you’ve never worked with local portfiles before, here’s a quick tutorial…

Mavericks, MacPorts, PostgreSQL 9, Tomcat 6 and PostgreSQL Studio

Now that Amazon Web Services is supporting PostgreSQL I figured it’s about time I got around to getting it setup locally.

First step was to install PostgreSQL via MacPorts:

You should now be able to connect to your database using pgAdmin or a similar tool. If you’re having trouble, a quick reboot of your machine should get it working (assuming you set PostgreSQL to load at startup).

I also wanted to try out PostgresSQL Studio which requires Tomcat so that got installed next:

Installing Tomcat led to a momentary headache as it would not start, throwing the following error – Apple AWT Java VM was loaded on first thread — can’t start AWT. All of the OS X / MacPorts / Tomcat instructions on Google were pretty out of date so it took a bit of digging to figure out what was going on. Luckily the fix was rather simple – just edit tomcatctl with the change shown in the Gist above.

The final step was to download PostgreSQL Studio, unzip the file and drop pgstudio.war into /opt/local/share/java/tomcat6/webapps/.

After a few moments Tomcat will autodeploy the war and you’ll be able to browse the application at http://localhost:8080/pgstudio/.

Celery 3.1 with Django, django-celery, RabbitMQ and MacPorts

Celery 3.1 was released earlier this week and with it comes Django support out of the box. This means Django users can now use the Celery API directly rather than depending on django-celery (the libary is still needed if you want to make use of the database result backend or the Django periodic task admin). This also means you can now use the celery command directly rather than going through

In this post I’ll cover the changes necessary to migrate to 3.1 from a previous installation as well as provide updated instructions for getting started from scratch. First, the migration…

1) Remove django-celery integration (even if you are using django-celery for the database result backend or the periodic task admin, you still need to do this). Remove the following from your Django settings (also remove from if present there):

2) If you’re using the default RabbitMQ broker (and default username / password), you won’t need this in your settings either:

3) To still use the database result backend from django-celery, ensure the following is in your Django settings:

4) Create an instance of the celery library for Django to use.

This code performs a number of functions:

  • sets the default DJANGO_SETTINGS_MODULE for the celery command-line program
  • creates an instance of the celery library
  • adds the Django settings module as a configuration source for Celery (allows you to configure Celery directly from your Django settings)
  • autodiscovers tasks within your installed apps

In my application structure I’ve placed this in a file called

5) If you want to make use of the shared_task decorator as demonstrated in the Django example application, add the following to your application’s

This decorator returns a proxy that always points to the currently active Celery instance, allowing you to define tasks within multiple applications and still use a single Celery instance.

That’s it for changes. Now the final piece is making use of new commands for running Celery. To run a worker, you previously used the following:

$ python celery worker --loglevel=info

This is replaced with:

$ celery -A app worker -l info

Similarly, running Celery beat:

$ python celery beat

Is now:

$ celery beat -A app

Note that each command requires the name of your Django application (the one that contains to be passed via -A.

For a fresh install, here’s a quick rundown of the 6 steps it takes to get up and running…

1) Install RabbitMQ

2) OPTIONAL – configure a rabbitmq user

3) Install Celery (django-celery is only necessary if you want to use the database result backend or the Django periodic task admin)

4) Configure your Django application (only necessary if using django-celery, be sure to also add django-celery to your installed apps)

5) Create an instance of the celery library for Django to use

In my application structure I’ve placed this in a file called

6) Sync your DB (only necessary if using django-celery)

You can now run Celery using the following commands:

$ celery -A app worker -l info

$ celery beat -A app

MacPorts & Mavericks OS X 10.9 & Gnutar Errors

UPDATE – The 10.9 Mavericks binary is now available here.

If you try installing or updating MacPorts ports after upgrading OS X to 10.9, you’ll hit an error with gnutar:

The recommended fix from MacPorts is to reinstall MacPorts and all ports after a major OS upgrade. Unfortunately there is no binary for Mavericks yet and quite a few ports are failing to build right now including MySQL 5.6 and Python 2.7.

The  workaround is to keep your current MacPorts installation and install gnutar from source:

This is definitely not a long term solution and should really just be used for new ports that are must haves. I wouldn’t run port upgrade outdated or try to upgrade any specific ports as you will find that not all ports will build.

Once a Maverick’s binary is out, be sure to remove this gnutar before doing the migration.

Read more discussion of MacPorts on Mavericks.

Note: Be sure to build and install gnutar from source and not just symlink…

Getting Celery Running with Django, django-celery, RabbitMQ and MacPorts

UPDATE: With the release of Celery 3.1, these instructions are out of date. I’ll be posting some notes on migration and doing a fresh 3.1 installation in the near future.

I recently needed an asynchronous task queue and found the fantastic Celery Project to be just what I was looking for.

Getting up and running was fairly simple as there’s lots of great documentation…

Django + Celery


But rather than read all of those, here’s a quick rundown of the 5 steps it takes to get up and running…

1) Install RabbitMQ

2) OPTIONAL – configure a rabbitmq user

3) Install Celery

4) Configure your Django application

5) Sync your DB

Boom goes the dynamite. You can now jump into the beginners Django tutorial found here.