Python环境

警告
本文最后更新于 2019-12-28,文中内容可能已过时。
  1. git clone pyenv的代码
1
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
  1. 定义环境变量PYENV_ROOT
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# For bash:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile

# For Ubuntu Desktop:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc

# For Zsh:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc

# For Fish shell:
set -Ux PYENV_ROOT $HOME/.pyenv
set -Ux fish_user_paths $PYENV_ROOT/bin $fish_user_paths
  1. pyenv init添加到shell中以启用shimsautocompletion功能。请确保将eval "$(pyenv init -)"放在shell配置文件的末尾,因为它在初始化期间会操纵PATH。
1
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

Zsh note: Modify your ~/.zshrc file instead of ~/.bash_profile.

fish note: Use pyenv init - | source instead of eval (pyenv init -).

Ubuntu and Fedora note: Modify your ~/.bashrc file instead of ~/.bash_profile.

  1. 刷新shell
1
exec "$SHELL"
  1. 安装编译环境

Install Python build dependencies

  1. 将Python版本安装到$(pyenv root)/version中。例如,要下载并安装Python 2.7.8,请运行:
1
pyenv install 3.9.1
1
2
cd $(pyenv root)
git pull
1
2
3
4
5
# 禁用
pyenv init

# 删除
rm -rf $(pyenv root)
1
2
mkdir ~/.pyenv/cache
version="3.9.1"; echo $version; wget "https://mirrors.huaweicloud.com/python/$version/Python-$version.tar.xz" -P ~/.pyenv/cache/; pyenv install $version
1
pyenv versions
1
2
3
4
5
6
7
8
# 下载
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

# 添加到环境变量
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc

# 刷新shell
exec "$SHELL"
1
2
3
# 指定版本或使用系统的版本
pyenv virtualenv [-f|--force] [VIRTUALENV_OPTIONS] [version] <virtualenv-name>
pyenv virtualenv 3.9.1 mypython
1
2
pyenv activate <name>
pyenv deactivate

Pyenv - Simple Python version management

virtualenv is a tool to create isolated Python environments

pyenv-virtualenv

PEP 582 – Python local packages directory

You don’t really need a virtualenv