安装环境准备
- 准备安装pyenv的环境:
aptitude install curl aptitude install git
- 准备安装python的环境:
aptitude install clang aptitude install binutils aptitude install build-essential aptitude install zlibc aptitude install libssl-dev aptitude install zlib1g-dev aptitude install libncurses5-dev aptitude install libncursesw5-dev aptitude install libreadline-dev aptitude install libsqlite3-dev aptitude install libgdbm-dev aptitude install libdb5.3-dev aptitude install libbz2-dev aptitude install libexpat1-dev aptitude install liblzma-dev aptitude install tk-dev
Python版本安装
- 安装python3.6.3:
$ pyenv install 3.6.3 Downloading Python-3.6.3.tar.xz... -> https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz Installing Python-3.6.3... Installed Python-3.6.3 to /home/ubuntu/.pyenv/versions/3.6.3
- 卸载Python3.6.3:
pyenv uninstall 3.6.3
Python版本管理
- 查看系统当前使用到的版本:
$ pyenv version system (set by /home/ubuntu/.pyenv/version)
- 查看系统所有版本:
$ pyenv versions * system (set by /home/ubuntu/.pyenv/version) 3.6.3
- 版本设置有如下三种方式:
其中:pyenv shell 3.6.3 pyenv local 3.6.3 pyenv global 3.6.3
1.pyenv shell 3.6.3
设置环境变量PYENV_VERSION
,仅应用于当前Shell,退出Shell则恢复;
1.pyenv local 3.6.3
在当前目录下创建文件.python-version
,其中内容为版本信息3.6.3
,进入该目录则版本切换为3.6.3;
1.pyenv global 3.6.3
在目录~/.pyenv
下创建文件version
,其内容为版本信息3.6.3
;
版本搜索路径为:
1. 首先检查是否有设置环境变量PYENV_VERSION
,有设置则以此为准;
1. 前者不符检查当前目录是否存在文件.python-version
,有设置则以此为准;
1. 前两者不符则检查是否存在文件~/.pyenv/version
,有设置则以此为准;
1. 前三者均为设置,则以系统设置为准;
pyenv管理virtualenv
- 切换版本:
pyenv shell 3.6.3
- 创建虚拟环境:
$ pyenv virtualenv stock Requirement already satisfied: setuptools in /home/ubuntu/.pyenv/versions/3.6.3/envs/stock/lib/python3.6/site-packages Requirement already satisfied: pip in /home/ubuntu/.pyenv/versions/3.6.3/envs/stock/lib/python3.6/site-packages
- 进入虚拟环境:
$ pyenv activate stock pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
- 退出虚拟环境:
$ source deactivate pyenv-virtualenv: deactivate 3.6.3/envs/stock