Using External Python Modules and the site-packages Directory

The site-packages directory

Modo doesn’t really know about the Python site-packages directory. It doesn’t define one of it’s own and the rewriting of the sys.path variable at script startup means that the site-packages folder of your ‘default’ system python installation is invisible, along with any 3rd party packages and modules it contains. To access (parse) your system site-packages directory and make available any packages/modules inside it add the following code to the top of your script. This will also parse any .pth files, treating the site-packages directory just like regular python does:

1
2
3
4
5
6
7
8
import sys
from site import addsitedir

sitedir = r"C:\Python26\Lib\site-packages"
try:
    addsitedir(sitedir)
except:
    sys.exit(0)

That helps for ‘in-house’ use where hard coding the path to the system site-packages directory is a viable option. But what about distributing scripts when you can’t be sure whether end users even have a system installed python distribution, still less whether they have the package/module you’re interested in? There are several options with varying degrees of ease of use for end users:

  • point your script at the system site-packages folder, requiring end users to install, or have installed, Python 2.6 and the required package/module.

  • ship the package/module with your script and use modo’s path alias and import directives to add the package/module to the script’s Python path at runtime.

  • Python