@@ -109,7 +109,11 @@ def _get_toolchain_unix_cflags(rctx, python_interpreter, logger = None):
109
109
stdout = pypi_repo_utils .execute_checked_stdout (
110
110
rctx ,
111
111
op = "GetPythonVersionForUnixCflags" ,
112
- python = python_interpreter ,
112
+ # python_interpreter by default points to a symlink, however when using bazel in vendor mode,
113
+ # and the vendored directory moves around, the execution of python fails, as it's getting confused
114
+ # where it's running from. More to the fact that we are executing it in isolated mode "-I", which
115
+ # results in PYTHONHOME being ignored. The solution is to run python from it's real directory.
116
+ python = python_interpreter .realpath ,
113
117
arguments = [
114
118
# Run the interpreter in isolated mode, this options implies -E, -P and -s.
115
119
# Ensures environment variables are ignored that are set in userspace, such as PYTHONPATH,
@@ -198,6 +202,37 @@ def _parse_optional_attrs(rctx, args, extra_pip_args = None):
198
202
199
203
return args
200
204
205
+ def _get_python_home (rctx , python_interpreter , logger = None ):
206
+ """Get the PYTHONHOME directory from the selected python interpretter
207
+
208
+ Args:
209
+ rctx (repository_ctx): The repository context.
210
+ python_interpreter (path): The resolved python interpreter.
211
+ logger: Optional logger to use for operations.
212
+ Returns:
213
+ String of PYTHONHOME directory.
214
+ """
215
+
216
+ return pypi_repo_utils .execute_checked_stdout (
217
+ rctx ,
218
+ op = "GetPythonHome" ,
219
+ # python_interpreter by default points to a symlink, however when using bazel in vendor mode,
220
+ # and the vendored directory moves around, the execution of python fails, as it's getting confused
221
+ # where it's running from. More to the fact that we are executing it in isolated mode "-I", which
222
+ # results in PYTHONHOME being ignored. The solution is to run python from it's real directory.
223
+ python = python_interpreter .realpath ,
224
+ arguments = [
225
+ # Run the interpreter in isolated mode, this options implies -E, -P and -s.
226
+ # Ensures environment variables are ignored that are set in userspace, such as PYTHONPATH,
227
+ # which may interfere with this invocation.
228
+ "-I" ,
229
+ "-c" ,
230
+ "import sys; print(f'{sys.prefix}', end='')" ,
231
+ ],
232
+ srcs = [],
233
+ logger = logger ,
234
+ )
235
+
201
236
def _create_repository_execution_environment (rctx , python_interpreter , logger = None ):
202
237
"""Create a environment dictionary for processes we spawn with rctx.execute.
203
238
@@ -210,6 +245,7 @@ def _create_repository_execution_environment(rctx, python_interpreter, logger =
210
245
"""
211
246
212
247
env = {
248
+ "PYTHONHOME" : _get_python_home (rctx , python_interpreter , logger ),
213
249
"PYTHONPATH" : pypi_repo_utils .construct_pythonpath (
214
250
rctx ,
215
251
entries = rctx .attr ._python_path_entries ,
0 commit comments