Installing gevent inside a virtual environment on OSX

Installing Python’s gevent package can be a bit challanging. This blog
post explains how to install it on OSX v10.8 or later without using something like ‘MacPorts‘.

You will need to get XCode from Apple’s App Store and make sure to install the
Command Line Tools‘. You do this under Preferences>Downloads (Using XCode 5 you don’t have to do that anymore, apparently).

Installing virtualenv can be done using easy_install (or pip if available):

shell> easy_install virtualenv

Here’s how you can use virtualenv:

shell> mkdir MyExample
shell> cd MyExample
shell> virtualenv ENV
shell> source ENV/bin/activate
(ENV)shell>

The virtualenv package comes with pip, so we’ll use this instead of easy_install.

We need to install the C-library libevent. Download the latest version and do the following while in the virtual environment:

(ENV)shell> curl -L -O https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
(ENV)shell> tar xzf libevent-2.0.21-stable.tar.gz
(ENV)shell> cd libevent-2.0.21-stable
(ENV)shell> ./configure --prefix="$VIRTUAL_ENV"
(ENV)shell> make && make install
(ENV)shell> cd $VIRTUAL_ENV/..

If all went well, install the Pyton packages greenlet and gevent. Note that the order is important.

(ENV)shell> pip install greenlet
(ENV)shell> pip install gevent --global-option "-L$VIRTUAL_ENV/lib" \
    --global-option "-I$VIRTUAL_ENV/include"

The order is important because gevent depends on greenlet. If you install gevent first, the extra global options will not work and you’ll get an error.

Try to import gevent and if all went well, no error should raise:

shell> python
python> import gevent

Using OSX ‘Mavericks’ v10.9-beta you need XCode v5 Developer Preview to be able to compile libevent.

Compiling Python 3.2 with readline support on MacOS X 10.7

This post explains how to compile Python v3.2 including support for the GNU Readline Library on MacOS X 10.7 (Lion), not using tools like MacPorts. One of the problems we try to solve is making history work in the interactive Python interpreter.

Pre-requirement is to have Apple’s XCode installed (check the Appstore).

First, you need a fresh copy of the GNU Readline Library and compile it. At the time of writing, readline v6.2 was the latest. Fire up a Terminal and do the following:

shell> tar xzf readline-6.2.tar.gz
shell> cd readline-6.2
shell> MACOSX_DEPLOYMENT_TARGET=10.7 ARCHFLAGS="-arch x86_64" \
       ./configure --prefix=/opt/local
shell> make && sudo make install

Note that you can change the location /opt/local to anything you like, but avoid /usr/lib or other system location.

When readline compiled and installed successfully, compile Python v3.2 as follows:

shell> tar xzf Python-3.2.3.tgz
shell> cd Python-3.2.3
shell> LDFLAGS="-L/opt/local/lib" CFLAGS="-I/opt/local/include" \
       ./configure --prefix=/opt/python/3.2.3
shell> make && sudo make install

Note that you might have more arguments to pass to the ./configure if you have special needs. Note also that /opt/python/3.2.3 is where Python will be installed to avoid clashing with the system provided Python installations.

You should now have history support in the Python interpreter:

shell> /opt/python/3.2.3/bin/python3
>>> import this
>>>

Make your hostname stick in MacOSX

Using MacOSX, when your ISP or wireless access point is changing your hostname, make it sticky editing /etc/hostconfig.

Add or change the following line in /etc/hostconfig to reflect your hostname:

HOSTNAME="your hostname"

You could use Terminal.app to do this, but using Finder works too.

  1. Open a Finder window.
  2. Using the Menu: Go > Go to Folder..
  3. In the dialog that’s opening, type: /etc/
  4. /etc/ should now be available in your Finder window.
  5. Locate the hostconfig-file and open it using your favorite text editor (e.g. TextEdit)
  6. Add or change this line to reflect your hostname: HOSTNAME="your hostname"
  7. Save it, enter your password, and be happy.

Python, oursql and MacOS X 10.6 (Snow Leopard)

This post explains how to compile oursql and install it on MacOS 10.6. oursql is a Python database interface for MySQL, an alternative to MySQL for Python (i.e. MySQLdb) and MySQL Connector/Python.

First, find out which MySQL you installed. This can be either the 32-bit or the 64-bit version. To make sure, find the mysqld (e.g. in /usr/local/mysql/bin) and do the following in a Terminal window:

shell> file /usr/local/mysql/bin/mysqld
.../mysqld: Mach-O 64-bit executable x86_64

If you see x86_64, you got 64-bit, otherwise 32-bit. If you see both, then you have a universal build. This is important for specifying the ARGSFLAG when building.

Download oursql from Launchpad and unpack it into some directory. Using the information from above, you’ll have to do following for 64-bit platform (or universal build) in a Terminal window:

shell> ARCHFLAGS="-arch x86_64" python setup.py build
shell> sudo python setup.py install

For 32-bit, you’ll have to do:

shell> ARCHFLAGS="-arch i386" python setup.py build
shell> sudo python setup.py install

Following error will be reported when you don’t specify the correct ARCHFLAGS:

ld: warning: in .../lib/libmysqlclient.dylib,
file is not of required architecture

Tips:

  • When building failed, it is good to remove oursql, unpack it and try again.
  • If you don’t want to compile anything, or run into more troubles, give MySQL Connector/Python a try (alpha releases). It’s a pure Python implementation of the MySQL Client/Server protocol and doesn’t need compiling or a MySQL installation.
  • You can download MySQL from either www.mysql.com or dev.mysql.com.
By |February 8th, 2010|Python|0 Comments|

MySQL 5.1.42 available for MacOS X 10.6 (Snow Leopard)

A few days ago MySQL 5.1.42 got released and it is now available with builds for MacOS X 10.6 (Snow Leopard)! The download website doesn’t show it yet, but if you are burning to try, you can get it from the mirror-picking-website.

As usual, don’t forget to checkout the changelog before upgrading!

If you want to compile it yourself, and need a universal binary, you could try my previous blog entry «Building MySQL universal binaries using MacOS X 10.6 (Snow Leopard)».