Saturday, October 14, 2006

Adding .pth file support to IronPython

In a blog post I read, there was the following statement:

'Damnit. Apparently IronPython doesn’t support .pth files. I’m not sure if I should expect this or not from a “1.0” product, but it’s sure annoying since it seems most libraries use them.'

For me, it is an issue due the fact that many of the libraries I want to use come as Python Eggs, and the easy-install.pth file is required if you want them to work. So I decided to see what was needed to add .pth support to IronPython. In the end it was not too hard as the logic to add the contents of the pth files to the sys.path is included in the CPython site.py. It needed a little modification, but if you add this code to IronPython's site.py, .pth files work.

5 comments:

Patrick K. O'Brien said...

Great tip. Unfortunately, it didn't quite work for me. I had to add the following hack:

def addsitepackages(known_paths):
"""Add site-packages (and possibly site-python) to sys.path"""
## prefixes = [os.path.join(sys.prefix, "local"), sys.prefix]
sys_prefix = r'C:\Python24'
prefixes = [os.path.join(sys_prefix, "local"), sys_prefix]

Mark Rees said...

The unmodified version of addsitepackages assumes that the site packages are in the directory subtree of where IronPython is installed. So if IronPython was installed in C:\IronPython10, sys.prefix will return that directory path. But I think Patrick is wanting to use packages and .pth files in an existing installation of CPython 2.4 (which is a valid option), so his mod is good solution for that case.

Dave P said...

This is great news and glad to see it in fepy r4. However, I'm clueless as to how to use it! Does this mean that setuptools will work with IronPython now? How do I setup IronPython to recognize my .pth file and all the modules I've installed into cpython via setuptools?

Mark Rees said...

Hi Dave P,

I will create a new blog post next week that explains in more detail how it works, what is needed if you want to use a installed CPython site-packages directory, and the state of setuptools with IronPython.

Dave P said...

anxiously waiting...i want to get IronPylons working!