一、python多版本配置说明

安装python相关依赖

[root@centos6 ~]# yum install -y gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel

安装git工具

[root@centos6 ~]# yum install -y git

创建python用户

[root@centos6 ~]# useradd python

切换到python用户

[root@centos6 ~]# su - python

从GitHub上拉取pyenv脚本并执行

[python@centos6 ~]$ curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer|bash

通过加入以下内容到python用户的   ~/.bash_profile  文件中最后,使pyenv自动加载

export PATH="/home/python/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

使加入的内容生效

[python@centos6 ~]$ source ~/.bash_profile

命令行执行可以获取pyenv相关的命令说明

[python@centos6 ~]$ 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版本

[python@centos6 ~]$ python -V
Python 2.6.6

列出所有可用版本

[python@centos6 ~]$ pyenv install -l

安装指定版本

[python@centos6 ~]$ pyenv install 3.6.3 -v

注:

当这种在线方式安装太慢时,可以选择使用缓存安装,也就是把需要安装的对应源码包下好,放入/home/python/.pyenv/cache  目录下,cache目录不存在时创建即可

[python@centos6 ~]$ mkdir -p /home/python/.pyenv/cache
[python@centos6 ~]$ 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
[python@centos6 ~]$ pyenv install 3.6.3 -v

安装完成后,查看python版本(有 * 代码当前生效的python版本,system就是系统默认版本,这里是python2.6.6)

[python@centos6 ~]$ 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的功能

[python@centos6 ~]$ mkdir test    #在家目录新建一个test目录
[python@centos6 ~]$ ll
total 4
drwxrwxr-x 2 python python 4096 Nov 17 18:28 test
[python@centos6 ~]$ cd test/ #切换到test目录
[python@centos6 test]$ pyenv local 3.6.3
[python@centos6 test]$ pyenv versions
system
* 3.6.3 (set by /home/python/test/.python-version)
[python@centos6 test]$ python -V #这里应该是有bug,重新登录或就正常了
Python 2.6.6
[python@centos6 test]$ python -V
Python 3.6.3 #进入test的子目录,也继承了python版本设置
[python@centos6 test]$ mkdir child
[python@centos6 test]$ cd child/
[python@centos6 child]$ pyenv versions
system
* 3.6.3 (set by /home/python/test/.python-version)
[python@centos6 child]$ python -V
Python 3.6.3
#进入其他目录,python版本然是系统默认版本
[python@centos6 ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
[python@centos6 ~]$ python -V
Python 2.6.6

二、python多环境配置说明

创建一个3.6.3版本的独立空间环境

[python@centos6 ~]$ 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
[python@centos6 ~]$ pyenv versions #查看所有安装的python版本
* system (set by /home/python/.pyenv/version)
3.6.3
3.6.3/envs/martin
martin
[python@centos6 ~]$ cd test/
[python@centos6 test]$ pyenv local martin #设置当前目录的python版本
(martin) [python@centos6 test]$ pyenv versions #查看切换后的默认版本情况,设置独立空间环境后,命令提示符前面会多一个自己设置的独立空间的名字
system
3.6.3
3.6.3/envs/martin
* martin (set by /home/python/test/.python-version)

配置好独立空间后,安装的包存在独立目录下

(martin) [python@centos6 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) #独立空间的包存在该目录下

三、安装pip工具

切换到其他目录,当提示匹配命令不存在时,安装pip

官方文档:https://pip.pypa.io/en/stable/installing/

[python@centos6 ~]$ pip -V
pip 9.0.1 from /home/python/.pyenv/versions/3.6.3/lib/python3.6/site-packages (python 3.6)

[python@centos6 ~]$ pip -V
pip 9.0.1 from /usr/lib/python2.6/site-packages (python 2.6)

使用pip安装包

pip 通用配置,更改包搜索源
[python@abc ~]$ cat /home/python/.pip/pip.conf
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com

列出pip已安装的包
pip list

导出已安装的包到requirements格式文件里
pip freeze > requirement

按照所给的requirements格式文件中的包名按照包
pip install -r requirement

Linux下python多版本多环境介绍的更多相关文章

  1. linux下python多版本共存

    为何要安装python,linux下不是已经集成了python吗? 大多数linux系统都集成了python,但是他们的版本太低了.不能满足我们的需求,尤其是好多系统居然仍停留在 python2.6. ...

  2. Linux下python默认版本切换成替代版本

    本文链接自http://www.myhack58.com/Article/48/66/2016/71806.htm 当你安装 Debian Linux 时,安装过程有可能同时为你提供多个可用的 Pyt ...

  3. 将Linux下python默认版本切换成替代版本

    本文链接自http://www.myhack58.com/Article/48/66/2016/71806.htm 当你安装 Debian Linux 时,安装过程有可能同时为你提供多个可用的 Pyt ...

  4. 解决linux下python多版本兼容问题?

    环境:CentOS 7(7下默认安装python2.7) Pyhon3在CentOS中需要依赖一些其他的包,我们一次性用yum安装一下: yum install zlib-devel bzip2-de ...

  5. LINUX下搭建JAVA的开发环境

    LINUX下搭建JAVA的开发环境 (2009-07-13 10:04:13)     下面就将Linux下JAVA开发环境的搭建详细道来: 1.Linux下JDK的安装 至于下载JDK的二进制可执行 ...

  6. Linux学习心得之 Linux下命令行Android开发环境的搭建

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Linux学习心得之 Linux下命令行Android开发环境的搭建 1. 前言2. Jav ...

  7. Linux下python安装升级详细步骤 | Python2 升级 Python3

    Linux下python升级步骤  Python2 ->Python3 多数情况下,系统自动的Python版本是2.x 或者yum直接安装的也是2.x 但是,现在多数情况下建议使用3.x 那么如 ...

  8. windows+linux下jdk安装及java环境变量配置

    对于初学java的用户来说,可能第一件要做的事情就是安装jdk及配置环境,以下内容主要讲述windows及linux下jdk的安装以及环境变量的配置. 1.首先下载相应平台可用版本jdk安装文件,可以 ...

  9. [转]Windows下Python多版本共存

    https://blog.csdn.net/dream_an/article/details/51248736 Windows下Python多版本共存 Python数据科学安装Numby,pandas ...

随机推荐

  1. Js之全局函数

    全局函数 encodeURI()decodeURI()encodeURIComponent()decodeURIComponent()escape()unescape()eval() String() ...

  2. 维纳过程(Wiener Process)与高斯过程(Gaussian Process)

    维纳过程又叫布朗运动过程(Brownian motion): 1. 维纳过程 维纳过程 Wt 由如下性质所描述: W0=1, a.s.(a.s.,almost surely)

  3. WPF MVVM系列文章

    网上搜到了MSDN Magazine上Laurent Bugnion的系列文章. 以下为关于WPF的优秀实践,很有必要阅读. 很吸引人的标题有: IOC Containers and MVVM Mes ...

  4. 【狼窝乀野狼】Windows Server 2008 R 配置 Microsoft Server 2008 远程登录连接

    如果你已经了解了,或者你已经经历了,那么此篇文章对你是毫无用处.因为文笔深处未必有自己亲身体验来的真实有效. 闲话少说,直接上菜. 最近脑子“抽筋”,想安装一个服务器来玩玩,那么怎么选择呢?我的PC是 ...

  5. wpf 判断鼠标在一段时间内是否移动

    原文:wpf 判断鼠标在一段时间内是否移动 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/config_man/article/details/74 ...

  6. Oracle使用dblink连接SqlServer

    使用场景:当你需要从ORACLE数据库上访问另一台SqlServer数据库的数据时,Oracle提供了一个工具:gateways.通过这个工具,你可以创建dblink来连接sqlserver或其他不同 ...

  7. C#.NET自定义报表数据打印

    原文:C#.NET自定义报表数据打印 这是一个自定义的报表打印,是对PrintPreviewDialog的扩展和封装.PrintPreviewDialog是一个windows的打印预览控件,该类返回的 ...

  8. linuxC动态内存泄漏追踪方法

    C里面没有垃圾回收机制,有时候你申请了动态内存却忘记释放,这就尴尬了(你的程序扮演了强盗角色,有借有还才是好孩子).当你想找出内存泄露的地方时,有的投入海量的代码中,头痛不已.还好GNU C库提供了些 ...

  9. C# WebClient的使用

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  10. 命名管道的C#实现

    1.    命名管道简介 "命名管道"或"命名管线"(Named Pipes)是一种简单的进程间通信(I P C)机制,Microsoft Windows NT ...