Skip to main content

  

TLDR: sys.path does not contain FME_MF_DIR, so I can’t directly import a local Python module. This contradicts FME’s documentation. What am I missing?

 

Hi all,

I’m having problems importing a local Python module located in the same directory as my FME workspace. Could you help me find out what the problem is or am I missing something?

According to the PythonCaller documentation,

FME will search both the standard Python module locations and the workspace location to find the module to be imported.

However, when I try to import my module in <workspace directory>/feature_processor/__init__.py with the following code:

from feature_processor import FeatureProcessor

I get the following exception:

Message Type: fme::internal::_v0:🇵🇾:Exception
Python Exception <ModuleNotFoundError>: No module named 'feature_processor'
Error executing string `from feature_processor import FeatureProcessor
'
Factory proxy not initialized
FeatureProcessor (PythonFactory): PythonFactory failed to process feature
An error has occurred. Check the logfile above for details

 

To avoid this error, I’ve appended FME_MF_DIR to sys.path, but I’d like to understand why it is that I can’t directly import my module.

I’m currently using FME Form 2024.1.1.1 on Windows 10 x64

Thanks for the help.

Hi ​@marcel.garate ,

If the FeatureProcessor class is defined in this file saved in the same folder as the “__init__.py” file,

<workspace directory>/feature_processor/feature_processor.py

try writing this code into the “__init__.py”. Just be aware that the preceding . is required in this case. 

from .feature_processor import FeatureProcessor

 


Hi ​@takashi

Thank you for your reply. If I understand you correctly, this is for the relative import within my module. However, my problem is importing the module within FME Form. Once I get the path to my module in sys.path, I have no problem importing and running the FeatureProcessor class.


The preceding . is required if you publish classes/functions defined in other *.py files through "from ... import" statement in the "__init__.py". See the case 1 in the attached workspace examples.

In fact, there are several ways to import a custom pythom module saved in the workspace directory. In some cases, "__init__.py" is not required. See also other cases in the attachment.


Hi ​@takashi

Thank you for your very detailed answer, your examples helped me find the problem:

A workspace parameter of type “Scripted Value” somehow interferes with the appendment of the workspace path into sys.path. For this reason, this has to be done manually.