MCPcopy
hub / github.com/pex-tool/pex / from_env

Method from_env

pex/interpreter.py:1187–1227  ·  view source on GitHub ↗

Resolve a PythonInterpreter as /usr/bin/env would. :param hashbang: A string, e.g. "python3.3" representing some binary on the search path. :param paths: The search path to use; defaults to $PATH. :return: the first matching interpreter found or `None`.

(
        cls,
        hashbang,  # type: str
        paths=None,  # type: Optional[Iterable[str]]
    )

Source from the content-addressed store, hash-verified

1185
1186 @classmethod
1187 def from_env(
1188 cls,
1189 hashbang, # type: str
1190 paths=None, # type: Optional[Iterable[str]]
1191 ):
1192 # type: (...) -> Optional[PythonInterpreter]
1193 """Resolve a PythonInterpreter as /usr/bin/env would.
1194
1195 :param hashbang: A string, e.g. "python3.3" representing some binary on the search path.
1196 :param paths: The search path to use; defaults to $PATH.
1197 :return: the first matching interpreter found or `None`.
1198 """
1199
1200 def hashbang_matches(fn):
1201 basefile = os.path.basename(fn)
1202 return hashbang == basefile
1203
1204 for interpreter in cls._identify_interpreters(
1205 filter=hashbang_matches, error_handler=None, paths=paths
1206 ):
1207 return interpreter
1208
1209 if WINDOWS and hashbang.startswith("python"):
1210 # See: https://docs.python.org/3/using/windows.html#launcher
1211 env = os.environ.copy()
1212 env["PYLAUNCHER_DRYRUN"] = "1"
1213
1214 args = ["py"]
1215 version_part = hashbang[len("python") :]
1216 if version_part:
1217 args.append("-{version}".format(version=version_part))
1218
1219 with open(os.devnull, "wb") as fp:
1220 process = subprocess.Popen(args=args, env=env, stdout=subprocess.PIPE, stderr=fp)
1221 stdout, _ = process.communicate()
1222 if process.returncode == 0:
1223 # Windows is only supported for Python 3; so this cast is safe:
1224 result = cast(str, stdout.decode("utf-8").strip())
1225 return cls.from_binary(result)
1226
1227 return None
1228
1229 @classmethod
1230 def _spawn_from_binary(

Callers

nothing calls this directly

Calls 8

castFunction · 0.90
appendMethod · 0.80
communicateMethod · 0.80
stripMethod · 0.80
decodeMethod · 0.80
from_binaryMethod · 0.80
copyMethod · 0.45

Tested by

no test coverage detected