Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read default interpreter version from .python-version #1607

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/hatch/env/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down