How Does Python Locate Modules?
Python searches several locations for python modules using the search list specified in sys.path. To see what directories are in sys.path by default, you can run this code with python
import sys,pprint pprint.pprint(sys.path)
Code Re-Use in FME
In order to use your Python module from within FME, you need to do two things:
- Make sure python knows where the module can be found (i.e. make sure the module is in a directory referenced by sys.path)/li>
- Import the module
Module Locations
site-lib
Site-lib is the standard location for users to install their own modules. On Windows, it is generally located in:
c:\Python24\Lib\site-lib
Use the above instructions for printing sys.path if you cannot find your python installation's site-lib (on UNIX, this can vary).
Standard FME Python Directories
When FME spins up a python interpreter, it adds the following to sys.path:
- $(FME_HOME)/python
- $(FME_MF_DIR) (i.e. the location of the mapping file/workspace)
- Any location workbench recognizes as being a transformer directory (e.g."My Documents\FME\Transformers")
Other Places
If you would like python to search in other locations for your python script, there are two ways (via FME) to add directories to sys.path at runtime
1. Add snippet of code (similar to the following) to your workspace's Startup Python Script:
import sys sys.path.append(r"c:\project\foo")
2. Add the following to your workspace's header:
FME_PYTHON_PATH "c:\\project\\foo"
Importing Your Module
There are several ways you can tell FME to import a Python module, however, you may not even need to explicitly import your module.
If your module (for example mymodule.py ), contains the implementation of a PythonFactory ( for example MyFactoryClass), you need to tell the PythonFactory (PythonCaller transformer) that the "Python symbol to use" is mymodule.MyFactoryClass. The PythonFactory is smart enough to know that it needs to load the 'mymodule' module before it can use the implementing class.
If you want to import modules for other purposes, here's how you can do it:
In Your Startup Python Script add the following line:
import mymodule
In Your PythonCaller/PythonCreator add the following line:
import mymodule
In Your Mapping File or Workspace Header add the following line:
FME_PYTHON_IMPORT mymodule
Comments
0 comments
Please sign in to leave a comment.