From d62b3965be7434d75ed7590380b7879a57308462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez-Mondrag=C3=B3n?= Date: Wed, 10 Jul 2024 00:08:12 -0600 Subject: [PATCH] Read default interpreter version from `.python-version` Closes https://github.com/pypa/hatch/issues/1126 --- src/hatch/env/virtual.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/hatch/env/virtual.py b/src/hatch/env/virtual.py index dab5a7ee7..19aaa7f7a 100644 --- a/src/hatch/env/virtual.py +++ b/src/hatch/env/virtual.py @@ -222,10 +222,17 @@ def enter_shell(self, name: str, path: str, args: Iterable[str]): with self.expose_uv(), self.get_env_vars(): shell_executor(path, args, self.virtual_env.executables_directory) + def _read_dot_python_version(self) -> str: + path = self.root / '.python-version' + try: + return path.read_text().splitlines()[0].strip() + except Exception: # noqa: BLE001 + return '' + def check_compatibility(self): super().check_compatibility() - python_version = self.config.get('python', '') + python_version = self.config.get('python', '') or self._read_dot_python_version() if ( os.environ.get(AppEnvVars.PYTHON) or self._find_existing_interpreter(python_version) is not None @@ -252,6 +259,9 @@ def parent_python(self): if explicit_default := os.environ.get(AppEnvVars.PYTHON): return sys.executable if explicit_default == 'self' else explicit_default + if python_version := self._read_dot_python_version(): + return self._get_concrete_interpreter_path(python_version) + return self._get_concrete_interpreter_path() @cached_property