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))
The .python-version file should be placed in the root directory of your Python project—the same folder containing pyproject.toml, setup.py, or requirements.txt.
Example structure:
my_project/
├── .python-version <-- Here
├── .gitignore
├── pyproject.toml
├── src/
│ └── my_package/
└── tests/
To install Python 3.9 on Ubuntu:
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.9
Verify the installation:
python3.9 --version
This should output:
Python 3.9.13
.python-version is a simple text file used by version managers
to automatically switch to a specific Python version when you enter a project directory. Why Use It? Automation : No more manual pyenv local Consistency
: Ensures everyone on your team uses the exact same Python version. Portability .python version
: It works across different environments (Mac, Linux, and Windows with How to Create and Use It 1. Via Command Line (Recommended) If you have installed, navigate to your project folder and run: Use code with caution. Copied to clipboard This automatically creates a .python-version file in that folder containing the string 2. Manual Creation
You can create the file manually using any text editor (like VS Code or Vim): .python-version (Note the leading dot). : Only the version number (e.g., ). No extra spaces or text. Supported Tools Support Level Native; the standard for this file type. asdf-python respects this file. Native; a modern, faster alternative to asdf. GitHub Actions Often used with setup-python to auto-detect versions. Best Practices Git Tracking
: Always commit this file to your repository so other developers' environments sync automatically. Avoid Defaults : Don't just put ; use a specific version like to prevent "it works on my machine" bugs. : Keep it in the
of your project. Version managers will look upwards from subdirectories until they find it. Python.org Current Stable Versions (As of April 2026)
If you are starting a new project, consider these versions based on the official Python Release Schedule LTS/Stable — Current standard with significant performance gains.
— For testing the newest features like improved error messages.
— Use only if specific libraries are not yet compatible with 3.13. Python documentation Example Use Case: Installing Python 3
Should I help you set up pyenv or a specific environment manager to start using this file? What's New In Python 3.11 — Python 3.14.4 documentation
.python-version FileIf you are looking to generate or understand a .python-version file, this is a configuration file used by version managers (like pyenv) to automatically switch Python versions when entering a specific project directory.
How to create one: You can create this file manually or via CLI tools.
Using pyenv:
# Sets the local version to 3.10.5
pyenv local 3.10.5
This automatically creates a .python-version file in your current directory.
Manual Creation:
Create a file named .python-version and add the version number as the only content.
3.9.13
How it works:
When you cd into that directory, tools like pyenv, rye, or poetry read this file and instantly switch the active Python interpreter to that specific version. but you have 3.12 installed. Suddenly
Understanding Python versions and their features is essential for effective project management and development. By choosing the right Python version and following best practices, you can ensure your project's success and maintainability.
.python-version FilesPlace a .python-version file in each subproject's root. Tools that climb the directory tree will find the correct one.
monorepo/
├── service_a/
│ ├── .python-version # 3.10.4
│ └── pyproject.toml
├── service_b/
│ ├── .python-version # 3.11.5
│ └── pyproject.toml
If you need to check the version from inside a Python script (programmatically), use the sys or platform modules.
Code Snippet:
import sys
# Simple tuple output (good for logic checks)
print(sys.version_info)
# Output: sys.version_info(major=3, minor=11, micro=4, releaselevel='final', serial=0)
# Human-readable string
print(sys.version)
# Output: 3.11.4 (main, Jun 7 2023, 00:00:00) [GCC 9.4.0]
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.
To manage multiple Python versions on your system:
pyenv, conda, or virtualenv allow you to install and manage multiple Python versions.