How To Run Python.NET on Mac OS X Lion with Mono

Tuesday, January 17th 2012

Let’s get that Python running on Mono!

Install subversion : I used homebrew.

Install Python for .NET Source : Pick a nice directory for installation. I put it in ~/dev/pythonnet. I ran these commands.

mkdir -p ~/dev/
cd ~/dev
svn co pythonnet

Install Monodevelop : I installed version 2.8.5 because they don’t have a release for, yet.

Open Python.NET Solution : Start Monodevelop, answer its stupid questions, and open the solution. I found mine at ~/dev/pythonnet/pythonnet/pythonnet.sln.

Configure Build for Python 2.7 : Double-click the “Python.Runtime” project to open its optiosn. In the dialog that appears, select “Build > Compiler” from the navigation pane on the left. For each configuration in the Configuration dropdown, change the PYTHON26 in the “Define Symbols” input to PYTHON27.


Link the Python Shared Library to the Test Output Directory : Create a softlink from libpython.2.7.dylib to libpython27.dylib in the output directory of the “EmbeddingTest” project.

mkdir -p ~/dev/pythonnet/pythonnet/src/embed_tests/bin/Release/
cd ~/dev/pythonnet/pythonnet/src/embed_tests/bin/Release/ libpython27.dylib
ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib

Patch the Import Unit Test : In the “EmbeddingTest” project, open the PyImportTest fixture. In the SetUp method, you will see on line 28 a line that looks like a path to a “tests” directory. You need to change that because Python on Mac OS X does not know about those backslashes. So, change it to the following. (I’ve already submitted a patch to the project, so you may not have to complete this step if they choose to apply it to the source.)

char c = System.IO.Path.DirectorySeparatorChar;
string s = string.Format(@"..{0}..{0}..{0}tests", c);

Build Solution : You know, CTRL+COMMAND+B.

Run the Unit Tests One Fixture At A Time : This kind of blows. However, the Unix pipe used to communicate with the tests doesn’t do well with the embedded cPython runtime. They should all pass.

Now, you can use Python.NET in your Mono development. Just remember to link in that shared library or put it in your MONO_PATH environment variable.