How to manage python versions on windows

文章目录

      • [Using the py.exe Launcher](#Using the py.exe Launcher)
        • [Installation and Setup](#Installation and Setup)
        • [Basic Usage](#Basic Usage)
      • [Using pyenv-win](#Using pyenv-win)
      • [Other Considerations](#Other Considerations)

Managing multiple Python versions on Windows can be done through a few reliable methods, depending on your needs for simplicity, flexibility, or project isolation. I'll cover the two most popular approaches: the built-in py.exe launcher (which comes with Python 3 installations) and pyenv-win (a port of the Unix pyenv tool). These allow you to install, switch, and run different versions without conflicts. For project-specific isolation, combine these with Python's built-in venv module to create virtual environments tied to specific versions.

Using the py.exe Launcher

The py.exe launcher is a lightweight tool included with Python 3 installations on Windows. It helps discover installed Python versions, select specific ones for execution, and handle scripts with shebang lines. It's ideal if you just need to run different versions without advanced management.

Installation and Setup
  • When installing Python from the official website (python.org), ensure you select the option to install the launcher during setup. It's optional, so check the box for "py launcher" or similar.
  • If it's not installed, reinstall Python and enable it. The launcher will be added to your PATH automatically.
  • For older versions like Python 2.7, you may need to manually add registry keys for detection (under HKEY_LOCAL_MACHINE\Software\Python\PythonCore or user-specific equivalents) if they're missing.
Basic Usage
  • List installed Python versions : Run py -0 to see all detected versions and architectures (e.g., -3.9-64 * indicates the default). Use py -0p to include full paths to python.exe.

  • Run a specific version : Use flags like py -3.8 to run Python 3.8, or py -2 for the latest Python 2.x. For example:

    复制代码
    py -3.8 --version

    This outputs the version info for Python 3.8.

  • Run scripts : Execute a script with a specific version, e.g., py -3.7 myscript.py. If your script has a shebang (e.g., #!/usr/bin/env python3), running py myscript.py will automatically use the matching version.

  • Default behavior : Without flags, py uses the latest installed version. To check: py --version.

This method doesn't handle installing new versions---you'll need to download and install them manually from python.org---but it's great for switching between existing ones.

Using pyenv-win

For more comprehensive management, including easy installation and switching of versions, use pyenv-win. It's a Windows-specific tool that mirrors the Unix pyenv and lets you install, list, set global/local versions, and uninstall Pythons without affecting your system PATH.

Installation
  1. Open PowerShell (run as administrator if needed).

  2. Download and run the installation script:

    复制代码
    Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"
    • If you get an execution policy error, run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine first.
  3. Reopen PowerShell.

  4. Verify with pyenv --version.

  5. Update the version database: pyenv update.

  6. (Optional but recommended for Windows 10+): Disable built-in Python app aliases in Settings > Apps > Apps & features > App execution aliases, and turn off Python-related ones.

  7. Add these to your system PATH (via System Properties > Environment Variables) if not added automatically:

    • C:\Users\<YourUsername>\.pyenv\pyenv-win\bin
    • C:\Users\<YourUsername>\.pyenv\pyenv-win\shims
  8. Restart your terminal or IDE.

Basic Usage
  • List available versions to install : pyenv install --list (or pyenv install -l). Filter with pyenv install -l | findstr 3.8 for specific ones.
  • Install a version : pyenv install <version>, e.g., pyenv install 3.13.5. For multiple: pyenv install 3.13.5 3.14.0. Use -q for quiet mode (no prompts).
    • Supports 32-bit with suffixes like 3.8.2-win32.
  • List installed versions : pyenv versions.
  • Check current version : pyenv version.
  • Set versions :
    • Global (system-wide): pyenv global <version>, e.g., pyenv global 3.13.5. Use pyenv global system to revert to your system's default Python.
    • Local (project directory): pyenv local <version>. This creates a .python-version file in the folder for automatic switching.
    • Shell (current session): pyenv shell <version>. Unset with pyenv shell --unset.
  • Uninstall a version : pyenv uninstall <version>.
  • Rehash shims : After installing packages or switching versions, run pyenv rehash to update executable paths.
  • Run commands with a version : pyenv exec python myscript.py uses the current version's Python.
Tips
  • Always run pyenv rehash after changes to ensure tools like pip work correctly.
  • For project isolation, after setting a version, create a virtual environment: python -m venv myenv, then activate it.
  • pyenv-win builds versions from source, so installations can take time but support a wide range (Python 2.4+).

Other Considerations

  • Manual Management : Install versions from python.org into separate directories (e.g., C:\Python38), then call them explicitly like C:\Python38\python.exe myscript.py or adjust PATH temporarily. This is basic but error-prone for frequent switches.
  • Anaconda/Miniconda : If you work with data science, use Miniconda to install different Python versions via environments (e.g., conda create -n py38 python=3.8). It's heavier but includes package management.
  • Best Practices: Avoid modifying your system PATH globally for Pythons to prevent conflicts. Test versions in isolated environments, and keep your default system Python untouched for OS compatibility.

If you run into issues, check your environment variables or restart your terminal. For the latest updates, refer to the official pyenv-win repo or Python docs.

相关推荐
橙露7 分钟前
Python 对接 API:自动化拉取、清洗、入库一站式教程
开发语言·python·自动化
Omigeq14 分钟前
1.4 - 曲线生成轨迹优化算法(以BSpline和ReedsShepp为例) - Python运动规划库教程(Python Motion Planning)
开发语言·人工智能·python·算法·机器人
2301_8084143815 分钟前
自动化测试的实施
开发语言·python
无限码力19 分钟前
华为OD技术面真题 - Python开发 - 4
python·华为od·华为od技术面真题·华为od面试八股文·华为od面试真题·华为odpython开发真题·华为od技术面题目
波波00732 分钟前
写出稳定C#系统的关键:不可变性思想解析
开发语言·c#·wpf
dr_yingli1 小时前
fMRI(3-1)报告(个体化报告)生成器说明
开发语言·matlab
hrhcode1 小时前
【java工程师快速上手go】一.Go语言基础
java·开发语言·golang
l1t1 小时前
用wsl自带的python 3.10下载适用于3.12的pandas版本结合uv安装python 3.12模拟离线安装场景
python·pandas·uv
飞Link1 小时前
【AI大模型实战】万字长文肝透大语言模型(LLM):从底层原理解析到企业级Python项目落地
开发语言·人工智能·python·语言模型·自然语言处理
妙蛙种子3111 小时前
【Java设计模式 | 创建者模式】 原型模式
java·开发语言·后端·设计模式·原型模式