本文内容来自
https://pyenv-win.github.io/pyenv-win/#installation
windows 安装
1.如果本机已经安装python则使用pip安装
在命令行中执行以下命令(注意这里不要使用powershell ,我测试的时候发现它无法解析%USERPROFILE%):
pip install pyenv-win --target %USERPROFILE%\.pyenv
如果报错则使用以下命令(github issue):
pip install pyenv-win --target %USERPROFILE%\.pyenv --no-user --upgrade
使用powershell 添加pyenv 的相关环境变量
[System.Environment]::SetEnvironmentVariable(‘PYENV’,$env:USERPROFILE + “.pyenv\pyenv-win”,“User”)
[System.Environment]::SetEnvironmentVariable(‘PYENV_ROOT’,$env:USERPROFILE + “.pyenv\pyenv-win”,“User”)
[System.Environment]::SetEnvironmentVariable(‘PYENV_HOME’,$env:USERPROFILE + “.pyenv\pyenv-win”,“User”)
[System.Environment]::SetEnvironmentVariable(‘path’, $env:USERPROFILE + “.pyenv\pyenv-win\bin;” + $env:USERPROFILE + “.pyenv\pyenv-win\shims;” + [System.Environment]::GetEnvironmentVariable(‘path’, “User”),“User”)
2.如果本机没有安装python 则使用powershell直接安装
在powershell 中执行以下命令:
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”
如果报出下边这个错误,则需要以管理员身份运行powershell然后执行Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
,之后再运行上边一条命令。
& : File C:\Users\kirankotari\install-pyenv-win.ps1 cannot be loaded because running scripts is disabled on this system. For
more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:173
+ ... n.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
如果有其它错误,可以在github issue里找找。
使用
列出一些常用的命令
查看pyenv支持的python版本: pyenv install -l
上一条命令加上过滤: pyenv install -l | findstr 3.8
安装指定版本的python: pyenv install 3.5.2
注意:非静默安装可能会弹出安装窗口,不需要改动窗口中的配置,使用默认配置即可,也可以使用 -q
参数进行静默安装
也可以一次性安装多个版本: pyenv install 2.4.3 3.6.8
设置全局使用的python版本: pyenv global 3.5.2
注意: 这个指定的版本必须已经安装到本机
设置局部使用的python版本: pyenv local 3.5.2
卸载指定版本的python: pyenv uninstall 3.5.2
查看当前使用的python版本: pyenv version
查看本地安装的所有的python版本: pyenv versions
After (un)installing any libraries using pip or modifying the files in a version’s folder, you must run pyenv rehash
to update pyenv with new shims for the python and libraries’ executables.
注意: 这个命令必须在 .pyenv 文件夹之外.
Mac 安装
与windows类似
brew install pyenv
安装后,根据自己使用的终端,需要配置环境变量(github粘贴过来的):
- bash
First, add the commands to ~/.bashrc by running the following in your terminal:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc
Then, if you have ~/.profile, ~/.bash_profile or ~/.bash_login, add the commands there as well. If you have none of these, create a ~/.profile and add the commands there.
to add to ~/.profile:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
echo 'eval "$(pyenv init - bash)"' >> ~/.profile
to add to ~/.bash_profile:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init - bash)"' >> ~/.bash_profile
Bash warning: There are some systems where the BASH_ENV variable is configured to point to .bashrc. On such systems, you should almost certainly put the eval “$(pyenv init - bash)” line into .bash_profile, and not into .bashrc. Otherwise, you may observe strange behaviour, such as pyenv getting into an infinite loop. See #264 for details.
- Zsh
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init - zsh)"' >> ~/.zshrc
If you wish to get Pyenv in noninteractive login shells as well, also add the commands to ~/.zprofile or ~/.zlogin.
- Fish
If you have Fish 3.2.0 or newer, execute this interactively:
set -Ux PYENV_ROOT $HOME/.pyenv
fish_add_path $PYENV_ROOT/bin
Otherwise, execute the snippet below:
set -Ux PYENV_ROOT $HOME/.pyenv
set -U fish_user_paths $PYENV_ROOT/bin $fish_user_paths
Now, add this to ~/.config/fish/config.fish:
pyenv init - fish | source
在使用pyenv时,我这边一直卡在download ,进行不下去,直接在浏览器中打开链接是可以正常下载的。后边在网上搜到一个技巧,手动创建目录~/.pyenv/cache
,然后将下载的文件放在该文件夹下,再执行 pyenv install xxxxx
即可从上述的目录直接安装
mkdir -p ~/.pyenv/cache