1.安装pyenv
1.1 安装依赖环境
yum install -y git
yum -y install gcc zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel git
1.2 安装pyenv包
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
1.3 配置环境变量
cat ~/.bashrc
export PATH="/root/.pyenv/bin:PATH"
eval "(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
source ~/.bashrc
1.4 验证pyenv
root@python ~]# pyenv
pyenv 1.2.18
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
activate Activate virtual environment
commands List all available pyenv commands
deactivate Deactivate virtual environment
doctor Verify pyenv installation and development tools to build pythons.
exec Run an executable with the selected Python version
global Set or show the global Python version(s)
help Display help for a command
hooks List hook scripts for a given pyenv command
init Configure the shell environment for pyenv
install Install a Python version using python-build
local Set or show the local application-specific Python version(s)
prefix Display prefix for a Python version
rehash Rehash pyenv shims (run this after installing executables)
root Display the root directory where versions and shims are kept
shell Set or show the shell-specific Python version
shims List existing pyenv shims
uninstall Uninstall a specific Python version
version Show the current Python version(s) and its origin
--version Display the version of pyenv
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
virtualenv Create a Python virtualenv using the pyenv-virtualenv plugin
virtualenv-delete Uninstall a specific Python virtualenv
virtualenv-init Configure the shell environment for pyenv-virtualenv
virtualenv-prefix Display real_prefix for a Python virtualenv version
virtualenvs List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
whence List all Python versions that contain the given executable
which Display the full path to an executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
2 pyen使用
2.1 安装python指定版本
pyenv install -l
pyenv install 3.7.6 -v
查看当前可用的python版本
[root@python projects]# pyenv versions
system
* 3.7.6 (set by PYENV_VERSION environment variable)
#查看当前使用的版本
pyenv version
2.2 配置及管理python版本
pyenv管理python版本的三个基础命令
pyenv global <version> #全局设置,基本不用
pyenv shelll <version> #当前shell下设置,关闭shell再连失效
pyenv local <version> #只针对当前路径下生效,利于开放使用
#pyenv local使用
mkdir magedu/projects -p
cd magedu/
cd projects/
pyenv local 3.7.6
[root@python ~]# python -V
Python 2.7.5
[root@python ~]# cd magedu/
[root@python magedu]# ll
总用量 0
drwxr-xr-x. 2 root root 29 5月 26 21:56 projects
[root@python magedu]# cd projects/
[root@python projects]# python -V
Python 3.7.6
3 python的虚拟环境
为了对不同的项目进行隔离,使每个项目使用独立的解释器及依赖,需要配置python虚拟环境.
[root@python magedu]# pyenv virtualenv 3.7.6 my3.7.6
Looking in links: /tmp/tmpd1axia5b
Requirement already satisfied: setuptools in /root/.pyenv/versions/3.7.6/envs/my3.7.6/lib/python3.7/site-packages (41.2.0)
Requirement already satisfied: pip in /root/.pyenv/versions/3.7.6/envs/my3.7.6/lib/python3.7/site-packages (19.2.3)
[root@python magedu]# pyenv versions
* system (set by /root/.pyenv/version)
3.7.6
3.7.6/envs/my3.7.6
my3.7.6
[root@python magedu]# mkdir -p projects/web
[root@python magedu]# cd projects/web/
[root@python web]# pyenv local my3.7.6
(my3.7.6) [root@python web]# cd ..
[root@python projects]#