Python 安装 pyenv 版本管理(Windows)

82 阅读2分钟

一、简介

  • 这变制用 git bash 安装成功,使用无问题。其他方式没测试。

  • 找到 pyenv-win 安装文档

    这里选择的是 Git Commands 安装方式,使用 git bash 就可以完成安装,命令会方便不少,尤其是会 mac/liunx 的用户。

    推荐使用 # Windows Terminal 内置 Git Bash,比很多第三方工具好用。

二、安装

  • 在使用 Git Bash 进行安装时,需要注意下:

    pyenv-win Git Commands 安装文档

    # 这是官方安装文档提供的命令,执行后发现这样执行有问题,所以进行了调整
    $ git clone https://github.com/pyenv-win/pyenv-win.git "%USERPROFILE%\.pyenv"
    
    # 调整为 C 盘用户文件夹,其实路径也是可以自由存放的,只是在配置环境变量的时候需要配置成自定义文件夹的路径就行。
    $ git clone https://github.com/pyenv-win/pyenv-win.git "C:\Users\Administrator\.pyenv"
    

    image.png

    image.png

    安装完成会多出一个 .pyenv 文件夹,然后就需要去配置环境变量了。

    image.png

  • 配置环境变量

    pyenv-win 环境变量配置文档

    • 可以测试下官方环境变量配置命令,配置的路径是否正确,可以在 PowerShell 中执行下面命令,会输出路径

      $ $env:USERPROFILE
      
      $ $env:USERPROFILE + ".pyenv\pyenv-win"
      

      image.png

      如果没什么问题,就依次执行官方提供的环境变量配置命令:

      image.png

      配置好后,需要重启命令行工具,关闭重新打开,执行测试命令:

      $ pyenv
      

      Git Bash 执行没问题,PowerShell 执行报错找不到命令,但是不影响我使用了,主要用 Git Bash

      image.png

三、常用命令

  • 安装,如果安装慢,可以 解释器(cmd) 换网络。

    $ pyenv install 3.11.0
    
  • 设置全局版本

    # 查看本地全部版本
    $ pyenv versions
    
    # 设置全局版本(全局永久,推荐)
    $ pyenv global 3.11.0
    
    # 设置本地版本(临时,窗口关闭失效)
    $ pyenv local 3.11.0
    

    测试版本

    $ python --version
    
  • 全部命令

    pyenv 3.1.1
    
    Usage: pyenv <command> [<args>]
    
    Some useful pyenv commands are:
       commands     List all available pyenv commands
       duplicate    Creates a duplicate python environment
       local        Set or show the local application-specific Python version
       latest       Print the latest installed or known version with the given prefix
       global       Set or show the global Python version
       shell        Set or show the shell-specific Python version
       install      Install a Python version using python-build
       uninstall    Uninstall a specific Python version
       update       Update the cached version DB
       rehash       Rehash pyenv shims (run this after installing executables)
       vname        Show the current Python version
       version      Show the current Python version and its origin
       version-name Show the current Python version
       versions     List all Python versions available to pyenv
       exec         Runs an executable by first preparing PATH so that the selected Python
       which        Display the full path to an executable
       whence       List all Python versions that contain the given executable
    
    See `pyenv help <command>' for information on a specific command.
    For full documentation, see: https://github.com/pyenv-win/pyenv-win#readme