pyenv虚拟环境安装
安装过程
配置yum源
# 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
安装git
# yum install -y git
安装Python环境依赖
# yum install -y gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel
安装python
# yum install python3
添加用户
# useradd python # echo 132456|passwd --stdin python # su - python
登录普通用户后安装pyenv
$curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash 由于网络问题导致无法安装,可以执行以下命令: git clone --depth 1 "git://github.com/pyenv/pyenv.git" "${HOME}/.pyenv" git clone --depth 1 "git://github.com/pyenv/pyenv-doctor.git" "${HOME}/.pyenv/plugins/pyenv-doctor" git clone --depth 1 "git://github.com/pyenv/pyenv-installer.git" "${HOME}/.pyenv/plugins/pyenv-installer" git clone --depth 1 "git://github.com/pyenv/pyenv-update.git" "${HOME}/.pyenv/plugins/pyenv-update" git clone --depth 1 "git://github.com/pyenv/pyenv-virtualenv.git" "${HOME}/.pyenv/plugins/pyenv-virtualenv" git clone --depth 1 "git://github.com/pyenv/pyenv-which-ext.git" "${HOME}/.pyenv/plugins/pyenv-which-ext"
如果出现curl: (35) SSL connect error的原因是nss版本过低,需要升级nss
# yum install -y nss
添加环境变量
$ 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版本
$ python -V Python 2.7.5
Pyenv的命令
$ 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
虚拟环境下安装Python3.5.3版本
1、从官网下载(非常慢,不建议)
$ 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
2、传包的方式
$ 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
pyenv版本控制
当前登录用户全局生效
[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,影响很大
修改当前shell的Python版本
[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的时候版本就变回原来的了,还需要重新再设置一次,非常麻烦,推荐使用下面的方法
Local(当前文件夹生效)
创建项目目录
[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
虚拟环境(解决打包问题)
安装3.5.3版本
[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版本
[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
posted on 2020-07-18 23:48 hopeless-dream 阅读(303) 评论(0) 编辑 收藏 举报