一、python多版本配置说明
安装python相关依赖
[[email protected] ~]# yum install -y gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel
安装git工具
[[email protected] ~]# yum install -y git
创建python用户
[[email protected] ~]# useradd python
切换到python用户
[[email protected] ~]# su - python
从GitHub上拉取pyenv脚本并执行
[[email protected] ~]$ curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer|bash
Load pyenv automatically by adding
# the following to ~/.bash_profile:
通过加入以下内容到python用户的 ~/.bash_profile 文件中最后,使pyenv自动加载
export PATH="/home/python/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
使加入的内容生效
[[email protected] ~]$ source ~/.bash_profile
命令行执行可以获取pyenv相关的命令说明
[[email protected] ~]$ pyenv pyenv 1.1.5 Usage: pyenv <command> [<args>] Some useful pyenv commands are: commands List all available pyenv commands local Set or show the local application-specific Python version global Set or show the global Python version shell Set or show the shell-specific Python version install Install a Python version using python-build uninstall Uninstall a specific Python version rehash Rehash pyenv shims (run this after installing executables) version Show the current Python version and its origin versions List all Python versions available to pyenv which Display the full path to an executable whence List all Python versions that contain the given executable See `pyenv help <command>‘ for information on a specific command. For full documentation, see: https://github.com/pyenv/pyenv#readme
可以安装多个python版本,然后切换
查看当前python版本
[[email protected] ~]$ python -V Python 2.6.6
列出所有可用版本
[[email protected] ~]$ pyenv install -l
安装指定版本
[[email protected] ~]$ pyenv install 3.6.3 -v
注:
当这种在线方式安装太慢时,可以选择使用缓存安装,也就是把需要安装的对应源码包下好,放入/home/python/.pyenv/cache 目录下,cache目录不存在时创建即可
[[email protected] ~]$ mkdir -p /home/python/.pyenv/cache [[email protected] ~]$ ll /home/python/.pyenv/cache/ total 38724 -rw-r--r-- 1 python python 16974296 Nov 17 10:19 Python-3.6.3.tar.xz -rw-r--r-- 1 python python 22673115 Nov 17 10:19 Python-3.6.3.tgz [[email protected] ~]$ pyenv install 3.6.3 -v
安装完成后,查看python版本(有 * 代码当前生效的python版本,system就是系统默认版本,这里是python2.6.6)
[[email protected] ~]$ pyenv versions * system (set by /home/python/.pyenv/version) 3.6.3
切换设置python版本,一般建议用python local设置
pyenv global 3.6.3
注意:root用户下,尽量不适用global,会改变全局的python版本
pyenv shell 3.6.3
只作用于当前会话
python local 3.6.3
设置从当前工作目录开始向下递归继承该设置
这里演示python local的功能
[[email protected] ~]$ mkdir test #在家目录新建一个test目录 [[email protected] ~]$ ll total 4 drwxrwxr-x 2 python python 4096 Nov 17 18:28 test [[email protected] ~]$ cd test/ #切换到test目录 [[email protected] test]$ pyenv local 3.6.3 [[email protected] test]$ pyenv versions system * 3.6.3 (set by /home/python/test/.python-version) [[email protected] test]$ python -V #这里应该是有bug,重新登录或就正常了 Python 2.6.6 [[email protected] test]$ python -V Python 3.6.3 #进入test的子目录,也继承了python版本设置 [[email protected] test]$ mkdir child [[email protected] test]$ cd child/ [[email protected] child]$ pyenv versions system * 3.6.3 (set by /home/python/test/.python-version) [[email protected] child]$ python -V Python 3.6.3 #进入其他目录,python版本然是系统默认版本 [[email protected] ~]$ pyenv versions * system (set by /home/python/.pyenv/version) 3.6.3 [[email protected] ~]$ python -V Python 2.6.6
二、python多环境配置说明
创建一个3.6.3版本的独立空间环境
[[email protected] ~]$ pyenv virtualenv 3.6.3 martin #最后的空间名,自己随意指定 Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.6.3/envs/martin/lib/python3.6/site-packages Requirement already satisfied: pip in /home/python/.pyenv/versions/3.6.3/envs/martin/lib/python3.6/site-packages [[email protected] ~]$ pyenv versions #查看所有安装的python版本 * system (set by /home/python/.pyenv/version) 3.6.3 3.6.3/envs/martin martin [[email protected] ~]$ cd test/ [[email protected] test]$ pyenv local martin #设置当前目录的python版本 (martin) [[email protected] test]$ pyenv versions #查看切换后的默认版本情况,设置独立空间环境后,命令提示符前面会多一个自己设置的独立空间的名字 system 3.6.3 3.6.3/envs/martin * martin (set by /home/python/test/.python-version)
配置好独立空间后,安装的包存在独立目录下
(martin) [[email protected] test]$ pip -V pip 9.0.1 from /home/python/.pyenv/versions/3.6.3/envs/martin/lib/python3.6/site-packages (python 3.6) #独立空间的包存在该目录下
Virtualenv插件,在plugins/pyevn-virtualenv
创建一个3.6.3版本的独立空间
pyenv virtualenv 3.6.3 martin
pyenv local martin