PDA

View Full Version : How to extend LW Python



dulo
06-14-2012, 09:34 AM
I would like to import numpy and PIL.
How would I do this ?
Where to copy the python 2.7 win64 files of numpy and PIL for LW to find them ?

Celshader
06-26-2012, 09:47 PM
I would like to import numpy and PIL.
How would I do this ?
Where to copy the python 2.7 win64 files of numpy and PIL for LW to find them ?

My apologies; I just saw this thread.

There is a way to store numpy and PIL wherever you want and to still reference them in the script. Here's how.

If numpy and PIL are stored here:

C:\Python27\Lib\site-packages

Then you could append the path using sys. Example:



import sys
import lwsdk

## the following line tells Python where to look for stuff.
sys.path.append(r"C:\Python27\Lib\site-packages")

## now you can import modules located in the above path.
import numpy
import PIL

dulo
06-27-2012, 04:11 AM
Great thanks that helped me a lot !!!

tburbage
06-27-2012, 08:17 PM
You don't need to add site-packages to the python internal path explicitly: it always looks in this standard location. If you add single files, e.g. if you put foo.py in site-packages/, you can then import it directly:

import foo

However, if you create directories of files under site-packages, the interpreter will not automatically scan these, so you have to "give it a hint" by creating a .pth file in site-packages e.g. "myimports.pth" which lists the subdirectories you want appended to sys.path. So if the numpy package has a top-level dir 'numpy', you would add a line in the .pth file with 'numpy' in it, and then this would work:

import numpy

This doc describes the process in more detail:
http://docs.python.org/library/site.html

Of course you can also put libraries/packages in some neutral place (i.e. not inside a specific python version installation) and then "include" them by using the system environment variable PYTHONPATH.