How to Use a Specific Python Version Locally for Installing or Running Python Projects

May 16, 2025 (2mo ago)

In the Python ecosystem, version compatibility can make or break your project. Whether you're maintaining legacy code that requires Python 3.6 or experimenting with the latest features in Python 3.12, knowing how to manage multiple Python versions is an essential skill for developers.

This guide will walk you through different approaches to use specific Python versions for your projects.

πŸ€” Why Use Specific Python Versions?

πŸ’» Method 1: Using System Python Versions

If you have multiple Python versions installed, you can specify which one to use with explicit commands:

# Check installed Python versions
python --version
python3 --version
python3.8 --version
 
# Use a specific version
python3.9 -m pip install package_name
python3.9 script.py

This works but has limitationsβ€”it doesn't isolate package dependencies between projects.

πŸ› οΈ Method 2: Virtual Environments (The Recommended Approach)

Virtual environments solve the package isolation problem and can be created with specific Python versions.

πŸ“¦ Using venv (Python's built-in solution)

# Create a virtual environment with a specific Python version
python3.9 -m venv myenv
 
# Activate the environment
# On Windows:
myenv\Scripts\activate
# On macOS/Linux:
source myenv/bin/activate
 
# Now you're using Python 3.9
pip install package_name
python script.py

🧰 Using virtualenv

The virtualenv tool offers more flexibility:

# Install virtualenv
pip install virtualenv
 
# Create environment with specific Python
virtualenv -p python3.9 myenv
 
# Activate as shown above

πŸš€ Method 3: pyenv (The Developer's Choice)

pyenv is the gold standard for Python version management, allowing you to:

πŸ”§ Installation

# macOS
brew install pyenv
 
# Linux
curl https://pyenv.run | bash

⚑ Basic Usage

# List available Python versions
pyenv install --list
 
# Install a specific version
pyenv install 3.9.7
 
# Set global Python version
pyenv global 3.9.7
 
# Set version for current directory
pyenv local 3.8.12

πŸ† Creating Virtual Environments with pyenv

For the ultimate solution, combine pyenv with virtual environments:

# Install pyenv-virtualenv
# (On macOS it's included with pyenv)
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
 
# Create a virtual environment using a specific Python version
pyenv virtualenv 3.9.7 my-project-3.9.7
 
# Activate the environment
pyenv activate my-project-3.9.7
 
# Or automatically activate when entering the project directory
pyenv local my-project-3.9.7

🐳 Method 4: Docker Containers

For complete isolation, consider Docker:

# Example Dockerfile
FROM python:3.9
 
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
 
COPY . .
CMD ["python", "app.py"]

Run with:

docker build -t myproject .
docker run myproject

✨ Best Practices for Version Management

  1. Always specify exact version requirements πŸ“Œ in your requirements.txt or setup.py
  2. Document Python version requirements πŸ“ in your README
  3. Use a .python-version file πŸ“„ (for pyenv) in your project root
  4. Consider CI/CD testing πŸ”„ across multiple Python versions

🎯 Conclusion

The ability to select and manage specific Python versions is crucial for modern development workflows. Virtual environments combined with tools like pyenv provide the most flexible solution, ensuring you can run any Python project regardless of its version requirements.

By mastering these techniques, you'll eliminate "but it works on my machine" problems and create more portable, maintainable Python projects.

Happy coding! πŸŽ‰