.python-version Configuration
The DotPythonVersionConfigFile manages the .python-version file for Python
version managers.
Overview
Creates a .python-version file that:
- Specifies the Python version for the project
- Used by pyenv and similar version managers
- Automatically set to the first supported Python version from pyproject.toml
- Located at the project root
- Ensures consistent Python version across environments
Inheritance
Inherits from: StringConfigFile
What this means:
- String-based config file (single text content)
- Reads Python version from pyproject.toml
- Simple text file format
File Location
Path: .python-version (project root)
Extension: .python-version - Version file.
Filename: Empty string to produce .python-version.
Special filename handling:
get_filename()returns""get_file_extension()returns"python-version"- Combined:
.python-version
How It Works
Automatic Generation
When initialized via uv run pyrig mkroot, the file is created with:
- Version extraction: Reads
requires-pythonfrom pyproject.toml - First version: Uses the first supported Python version
- File creation: Writes the version to
.python-version
Version Configuration
The version is automatically determined from the requires-python field in
pyproject.toml. Pyrig extracts the first (minimum) supported Python version
and writes it to the file.
Example: If requires-python = ">=3.10", the file contains 3.10.
Usage
Automatic Creation
uv run pyrig mkroot
File Content
3.10
Just the version number, nothing else.
Using with pyenv
# pyenv automatically uses .python-version
cd /path/to/project
python --version # Uses version from .python-version
Using with other tools
- asdf: Reads
.python-version - mise: Reads
.python-version - rtx: Reads
.python-version
Dynamic Configuration
The version is determined from pyproject.toml:
[project]
requires-python = ">=3.12"
Pyrig extracts the first supported version (3.12) and writes it to
.python-version.
Best Practices
- Keep in sync: Ensure
.python-versionmatchesrequires-pythonin pyproject.toml - Commit the file: Include
.python-versionin version control - Use version managers: Install pyenv, asdf, mise, or similar tools to manage Python versions
- Update when needed: Re-run
uv run pyrig mkrootwhen updatingrequires-python