Skip to main content
Solved

Importing a module in the workspace's directory into PythonCaller

  • May 23, 2025
  • 4 replies
  • 88 views

marcel.garate
Participant
Forum|alt.badge.img+2

  

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::py::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.

Best answer by marcel.garate

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.

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

4 replies

takashi
Celebrity
  • May 24, 2025

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

 


marcel.garate
Participant
Forum|alt.badge.img+2
  • Author
  • Participant
  • May 26, 2025

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.


takashi
Celebrity
  • May 26, 2025

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.


marcel.garate
Participant
Forum|alt.badge.img+2
  • Author
  • Participant
  • Best Answer
  • May 26, 2025

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.