# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo # yum -y install yum-utils # yum install wget -y # wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# yum install -y git
# yum install -y gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel
# useradd python # echo 132456|passwd --stdin python # su - python
$ curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer|bash
$ vim .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH export PATH="/home/python/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" $ . .bash_profile
$ python -V Python 2.7.5
$ pyenv pyenv 1.2.20 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
$ pyenv install 3.5.3 -v /tmp/python-build.20200718063111.6245 ~ Downloading Python-3.5.3.tar.xz... -> https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tar.xz
$ ls -a . .. .bash_history .bash_logout .bash_profile .bashrc .pki .pyenv .viminfo $ mkdir .pyenv/cache 将下载好的包放入cache目录,然后执行安装命令 $ pyenv install 3.6.4 -v /tmp/python-build.20200718070338.7001 ~/.pyenv /tmp/python-build.20200718070338.7001/Python-3.6.4 /tmp/python-build.20200718070338.7001 ~/.pyenv Installing Python-3.6.4... checking build system type... x86_64-pc-linux-gnu
[python@localhost .pyenv]$ pyenv version system (set by /home/python/.pyenv/version) [python@localhost .pyenv]$ python -V Python 2.7.5
$ pyenv version 3.6.4 (set by /home/python/.pyenv/version) $ python -V Python 2.7.5
[python@localhost ~]$ pyenv versions system * 3.6.4 (set by /home/python/.pyenv/version) [python@localhost ~]$ python -V Python 3.6.4
[python@localhost ~]$ pyenv global system [python@localhost ~]$ pyenv versions * system (set by /home/python/.pyenv/version) 3.6.4
注意:这里的global参数作用的是python用户,而非root用户。如果是root用户安装的,那么不要用global,影响很大
[python@localhost .pyenv]$ pyenv shell 3.6.4 [python@localhost .pyenv]$ pyenv versions system * 3.6.4 (set by PYENV_VERSION environment variable) [python@localhost .pyenv]$ python -V Python 2.7.5
[python@localhost ~]$ pyenv versions * system (set by /home/python/.pyenv/version) 3.6.4
[python@localhost ~]$ python -V Python 2.7.5
这种方法是当前shell生效的,如果当前shell关闭了,版本设置就无效了,再打开shell的时候版本就变回原来的了,还需要重新再设置一次,非常麻烦,推荐使用下面的方法
[python@localhost .pyenv]$ cd [python@localhost ~]$ mkdir zh/projects -p [python@localhost ~]$ cd zh/projects/
[python@localhost ~]$ cd zh/projects/
[python@localhost projects]$ pyenv local 3.6.4 [python@localhost projects]$ pyenv versions system * 3.6.4 (set by /home/python/zh/projects/.python-version) [python@localhost projects]$ python -V Python 3.6.4
[python@localhost cmdb]$ cd [python@localhost ~]$ pyenv versions * system (set by /home/python/.pyenv/version) 3.6.4 [python@localhost ~]$ python -V Python 2.7.5
[python@localhost projects]$ mkdir cmdb [python@localhost projects]$ cd zh/projects/cmdb [python@localhost cmdb]$ pyenv versions system * 3.6.4 (set by /home/python/zh/projects/.python-version) [python@localhost cmdb]$ python -V Python 3.6.4
[python@localhost ~]$ cd .pyenv/cache/ [python@localhost cache]$ ll total 31456 -rw-r--r--. 1 python python 15213396 Dec 7 2018 Python-3.5.3.tar.xz -rw-r--r--. 1 python python 16992824 Nov 17 2018 Python-3.6.4.tar.xz [python@localhost projects]$ pyenv install 3.5.3 -v [python@localhost ~]$ ll .pyenv/versions total 0 drwxr-xr-x. 6 python python 56 Jul 18 08:39 3.5.3 drwxr-xr-x. 6 python python 56 Jul 18 07:34 3.6.4
[python@localhost cmdb]$ pyenv virtualenv 3.5.3 zh353 Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.5.3/envs/zh353/lib/python3.5/site-packages Requirement already satisfied: pip in /home/python/.pyenv/versions/3.5.3/envs/zh353/lib/python3.5/site-packages
[python@localhost cmdb]$ pyenv versions system 3.5.3 3.5.3/envs/zh353 * 3.6.4 (set by /home/python/zh/projects/.python-version) zh353
[python@localhost cmdb]$ pyenv local zh353 (zh353) [python@localhost cmdb]$ pyenv versions system 3.5.3 3.5.3/envs/zh353 3.6.4 * zh353 (set by /home/python/zh/projects/cmdb/.python-version)
(zh353) [python@localhost cmdb]$ mkdir ../web (zh353) [python@localhost cmdb]$ cd ../web [python@localhost web]$ pyenv local 3.6.4 [python@localhost web]$ pyenv versions system 3.5.3 3.5.3/envs/zh353 * 3.6.4 (set by /home/python/zh/projects/web/.python-version) zh353
原文:https://www.cnblogs.com/zh-dream/p/13338226.html