Difference between revisions of "Using External Python Modules and the site-packages Directory"

From The Foundry MODO SDK wiki
Jump to: navigation, search
Line 19: Line 19:
 
* 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.
 
* 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.
 
* 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.
 +
 +
== More Information ==
 +
* [[Perl]]
  
 
[[Category: Scripting Languages]]
 
[[Category: Scripting Languages]]

Revision as of 13:51, 30 March 2013

Work in progress

The site-packages directory

Modo doesn't really know about the 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:

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.

More Information