ninia / jep

Embed Python in Java
Other
1.27k stars 146 forks source link

extend PyConfig to enable program name to be set via Py_SetProgramName #515

Open mike-hunhoff opened 5 months ago

mike-hunhoff commented 5 months ago

Is your feature request related to a problem? Please describe. I'm attempting to create an environment for Jep that mirrors that of the Python interpreter from which it was installed. This includes sys.prefix, sys.exec_prefix, sys.executable, sys._base_executable, etc. My understanding is that Python derives these and others from the path that is set via Py_SetProgramName. AFAIK Jep does not support configuring Python via Py_SetProgramName. Unfortunately I cannot rely on environment variables to determine how sys.prefix, etc. are set e.g. when running in a virtual environment.

Describe the solution you'd like I'd like to see PyConfig extended to include protected String programName set via public PyConfig setProgramName(String programName) { ... and passed to Py_SetProgramName before Py_Initialize. This should prompt the embedded Python interpreter to properly set sys.prefix, etc.

Describe alternatives you've considered I've considered setting sys.prefix, etc. manually from Python after Python has been initialized but it's nuanced and difficult to get the ordering correct. I'm open to suggestions here as well.

Additional context NA

bsteffensmeier commented 5 months ago

I am in favor of this change. Conceptually I think the jep PyConfig class should mirror the cpython PyConfig type wherever it is useful. The code for the new configuration should be almost identical to how we handle python home so it shouldn't be hard to figure out how to pass program name around. I think we should include this in the next release.

@mike-hunhoff If you would like to submit a PR that is the best way to ensure this gets included. Otherwise I have been meaning to update some code in that area to clean up deprecation warnings in newer Python versions so if I get to that I will also add this in.

mike-hunhoff commented 5 months ago

Thanks for the response @bsteffensmeier ! I'm not sure I'll be able to get to this before you but I'll keep it on my list in case I come across extra cycles. I think this would be a big win for configuring the embedded Python interpreter using Jep's API.