ninia / jep

Embed Python in Java
Other
1.28k stars 145 forks source link

ImportError: PyO3 modules may only be initialized once per interpreter process #481

Closed mike-hunhoff closed 11 months ago

mike-hunhoff commented 1 year ago

The following error occurs when attempting to use PyO3 modules with Jep:

ImportError: PyO3 modules may only be initialized once per interpreter process

See original issue here.

I am wondering if this is an incompatibility with Jep's use of a MainInterpreter?

bsteffensmeier commented 1 year ago

It looks like PyO3 does not support sub-interpreters. The recommended way to ensure compatibility with modules that do not support sub interpreters is to use SharedInterpreter instead of SubInterpreters.

Another workaround is to add any modules that are not compatible with sub-interpreters to the shared modules for every jep interpreter. When this is done the module is imported once, on the MainInterpreter and not imported in any other interpreter. It looks like you have already tried that, however I suspect your module may have components or dependencies that are incompatible with sub-interpreters and those may also need to be added to the list of shared modules. I have found it is often a cascading problem and it can require making many modules shared to get things working.

Since the MainInterpreter is not importing any modules other than the shared modules you specify, I do not think it is directly the source of the problem, it is more of a general problem with sub-interpreters and SharedInterpreter is the simplest solution.

mike-hunhoff commented 12 months ago

@bsteffensmeier thank you for your prompt response! Based on your feedback we have switched our tool to use SharedInterpreters in hopes this enables the use of all (C)Python modules.

mike-hunhoff commented 11 months ago

Switching to SharedInterpreter fixed this issue, thank you!