Pyenv使用

small parking
Table of Contents

安装

1. 安装程序

curl -fsSL https://pyenv.run | bash

bash 或zsh 使用哪个配置哪个即可

2. 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

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

3. 重启SHELL

exec “$SHELL”

使用

查看可使用的版本,前面带*表示当前使用的版本

pyenv versions

安装python3 版本 For example, to download and install Python 3.10.4, run:

pyenv install 3.10.4

切换到某个版本

pyenv global 3.10

Switch between Python versions To select a Pyenv-installed Python as the version to use, run one of the following commands:

pyenv shell <version> -- select just for current shell session
pyenv local <version> -- automatically select whenever you are in the current directory (or its subdirectories)
pyenv global <version> -- select globally for your user account

卸载

pyenv uninstall <versions>.

pyenv 管理虚拟环境

创建venv

这里使用pyenv 建立虚拟环境

pyenv virtualenv venv1

手动启用虚拟环境

pyenv activate venv1
#pyenv versions

如果想要将一个虚拟环境自动和目录关联

很好用的功能,一定要试试

如果你希望 spirit_config 目录下自动使用 venv1,可以在该目录下创建 .python-version 文件:

echo "venv1" > .python-version

那么进入目录后 会自动切换python 和虚拟环境