@@ -184,12 +184,10 @@ int main (int argc, char *argv[])
184184 flux_conf_builtin_get ("lua_path_add" , flags ));
185185 environment_push (env , "LUA_PATH" , getenv ("FLUX_LUA_PATH_PREPEND" ));
186186
187- environment_from_env (env , "PYTHONPATH" , "" , ':' );
188- environment_push (env ,
189- "PYTHONPATH" ,
190- flux_conf_builtin_get ("python_path" , flags ));
191- environment_push (env , "PYTHONPATH" , getenv ("FLUX_PYTHONPATH_PREPEND" ));
192-
187+ if ((s = getenv ("FLUX_PYTHONPATH_PREPEND" ))) {
188+ environment_from_env (env , "PYTHONPATH" , "" , ':' );
189+ environment_push (env , "PYTHONPATH" , s );
190+ }
193191 if ((s = getenv ("MANPATH" )) && strlen (s ) > 0 ) {
194192 environment_from_env (env , "MANPATH" , ":" , ':' );
195193 environment_push (env ,
@@ -333,6 +331,37 @@ void setup_path (struct environment *env, const char *argv0)
333331 log_msg_exit ("Unable to determine flux executable dir" );
334332}
335333
334+ void builtin_env_add_pythonpath (struct environment * env )
335+ {
336+ /* prepend to PYTHONPATH, which is no longer done by default:
337+ */
338+ environment_from_env (env , "PYTHONPATH" , "" , ':' );
339+ environment_push (env ,
340+ "PYTHONPATH" ,
341+ flux_conf_builtin_get ("python_path" , FLUX_CONF_AUTO ));
342+ environment_push (env , "PYTHONPATH" , getenv ("FLUX_PYTHONPATH_PREPEND" ));
343+ }
344+
345+ static void setup_python_wrapper_environment (void )
346+ {
347+ struct environment * env ;
348+ const char * val ;
349+
350+ /* Set FLUX_PYTHONPATH_ORIG to the current PYTHONPATH, then
351+ * prepend the builtin python_path to PYTHONPATH so the the
352+ * python wrapper py-runner.py can find the correct Flux
353+ * bindings. The wrapper will then reset PYTHONPATH to
354+ * FLUX_PYTHONPATH_ORIG to avoid polluting the user environment.
355+ */
356+ if (!(env = environment_create ()))
357+ log_err_exit ("error creating environment" );
358+ if ((val = getenv ("PYTHONPATH" )))
359+ environment_set (env , "FLUX_PYTHONPATH_ORIG" , val , ':' );
360+ builtin_env_add_pythonpath (env );
361+ environment_apply (env );
362+ environment_destroy (env );
363+ }
364+
336365/* Check for a flux-<command>.py in dir and execute it under the configured
337366 * PYTHON_INTERPRETER if found.
338367 */
@@ -361,6 +390,9 @@ void exec_subcommand_py (bool vopt,
361390 PYTHON_INTERPRETER ,
362391 wrapper ,
363392 path );
393+
394+ setup_python_wrapper_environment ();
395+
364396 execvp (PYTHON_INTERPRETER , av );
365397 }
366398 free (path );
0 commit comments