Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mike-hunhoff opened this issue Jul 10, 2023 · 3 comments

Comments

@mike-hunhoff
Copy link

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
Copy link
Member

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
Copy link
Author

@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
Copy link
Author

Switching to SharedInterpreter fixed this issue, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants