In your CI pipeline, you can read the file dynamically:
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version-file: '.python-version'
This ensures your CI runs the exact same version as developers.
If you are setting up a new environment, here is a quick reference for the "version" commands in popular Python tools: .python version
| Tool | Command to Install/Set Version | Config File Created |
| :--- | :--- | :--- |
| pyenv | pyenv local 3.10.0 | .python-version |
| Poetry | poetry env use 3.10 | poetry.lock / pyproject.toml |
| Rye | rye pin 3.10 | .python-version |
| Conda | conda install python=3.10 | environment.yml |
Create a file named .python-version at your project's root that pins the Python version for pyenv and compatible tools. Python 3
Example contents:
3.11.4
Notes:
Related search suggestions:
import random
import string
def generate_random_text(length=10):
"""Generates a random string of fixed length."""
letters = string.ascii_letters + string.digits + string.punctuation
return ''.join(random.choice(letters) for i in range(length))
if __name__ == "__main__":
# Generate and print a random string of length 20
print(generate_random_text(20))
Your colleague runs Python 3.10, but you have 3.12 installed. Suddenly, their use of datetime.UTC (new in 3.11) works fine on your machine but fails in CI. A .python-version file eliminates this ambiguity. Your colleague runs Python 3.10
Code formatters (black), linters (ruff), and type checkers (mypy) behave differently across major Python versions. Locking the version ensures reproducible linting results.