在centos6.5环境下搭建多版本python(python2.6、python2.7、python3.5)共存环境

来源:互联网 发布:unity3d小球下落很慢 编辑:程序博客网 时间:2024/05/21 20:25

可能存在的问题

  1. yum安装、源码安装、二进制安装用哪个,官网文档是源码安装,所以咱们就用源码安装
  2. 在源码安装的时候会有什么问题
    • 一个是默认路径的问题,在编译的时候时候如果不指定路径的话,很多二进制文件会安装到默认的目录下/usr/bin下面,系统原来的文件可能还会有,但是各种链接会指向新的二进制文件,这样有可能会造成系统内某些依赖原来版本python的服务依会出现问题,一个已知的是yum会出现问题。那么解决的方法就是在configure的时候制定一个不一样的路径,可以是/usr/local(这样pyhon的各个文件会散落在/usr/local的各个目录里面),可以是/usr/local/python3(这样python所有的文件都会集中到/usr/local/python3里面),当然也可以是任何你喜欢的地方比如(/home/user/Software/python3),放在哪里我觉得都可以,参考文档是放到/usr/local里面这样的好处是,默认的path路径里面就包含/usr/local,那么当你安装好之后,直接就可以调用命令而不用再修改PATH。其实我偏向于用/usr/local/python3。
    • make altinstall的问题,如果用make install,在安装完新版本的python之后,新旧两个版本的python可执行文件(python2 python3)都会被链接到python这个可执行文件上,这会出现一些问题。如果用make altinstall,在安装完之后,不会建立软链接。
  3. Unicode
    python经历了一个漫长和复杂的历史过程才支持了unicode,所以如果你没有别的什么特殊需求的话,在安装2.7的时候请务必添加UTF-32支持,虽然会增加内存的使用但是增加了兼容性。python3.3以上不存在这个问题。在configure的时候加上这句–enable-unicode=ucs4
  4. 动态库(shared library)
    现代所有的linux发行版都把python编程成共享库,有些第三方的工具像mod_wsgi或者Blender都是依赖于这个共享库的,所以在安装新版本的python的时候最好能把这个编译成共享库。在configure的时候加上LDFLAGS=”-Wl,-rpath /usr/local/lib”

前置安装

yum -y updateyum groupinstall 'Development Tools'yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel expat-devel

常规安装

Pyhon2.7

wget http://python.org/ftp/python/2.7.13/Python-2.7.13.tar.xztar xf Python-2.7.13.tar.xzcd Python-2.7.13./configure --prefix=/usr/local/python27 --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"make && make altinstal

Python3.6

wget http://mirrors.sohu.com/python/3.6.0/Python-3.6.0.tar.xz(国内sohu源)wget http://python.org/ftp/python/3.6.0/Python-3.6.0.tar.xztar xf Python-3.6.0.tar.xzcd Python-3.6.0./configure --prefix=/usr/local/python3 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"make && make altinstall

删除动态库中的符号以节省内存空间

strip /usr/local/lib/libpython2.7.so.1.0strip /usr/local/lib/libpython3.6m.so.1.0

安装pip,包管理

# First get the script:wget https://bootstrap.pypa.io/get-pip.py# Then execute it using Python 2.7 and/or Python 3.6:python2.7 get-pip.pypython3.6 get-pip.py# With pip installed you can now do things like this:pip2.7 install [packagename]pip2.7 install --upgrade [packagename]pip2.7 uninstall [packagename]

环境共存问题

利用virtualenv来解决多版本python共存的问题。virtualenv是python的一个包,在2.7下面需要用pip安装,在3.3以上已经内置在python里面了,不用在单独安装。我理解它的使用是这样的:
* 如果你想建立2.7版本下的隔离环境,你就需要用2.7版本下的vritualenv包的命令,如果你想建立3.6版本下的隔离环境就需要用3.6下的virtualenv包的命令。不同版本的virtualenv命令是不一样的。
* 排除版本不同的命令,virtual使用的大概框架是类似的
+ 首先创建一个虚拟环境,这个虚拟环境其实就是一个目录,这个目录里面是这个虚拟环境的配置文件,配置文件包括启动与结束这个虚拟环境的脚本以及库文件。你看这些目录其实就是那些那么版本文件的软链接嘛,比如2.7的虚拟环境,bin目录下基本上是2.7所有的那些二进制文件都链接过来。
+ 利用目录里面的启动脚本来启动虚拟环境,启动后在提示符前面会有一个这个虚拟环境的标识符。只要有这个标识符就说明在这个虚拟环境中了,你就可以自由的用python,pip这些命令再也不用担心出错了。
+ 利用目录里的结束脚本来结束虚拟环境。
其实看完这个目录树你就知道其实这是利用bash环境脚本等工具做出的一个子进程环境,其实自己做也行了。但是人家已经写好了就不要重新发明轮子,自己写还容易出错呢。

# Install virtualenv for Python 2.7 and create a sandbox called my27project:pip2.7 install virtualenvvirtualenv my27project# Use the built-in functionality in Python 3.6 to create a sandbox called my36project:python3.6 -m venv my36project# Check the system Python interpreter version:python --version# This will show Python 2.6.6# Activate the my27project sandbox and check the version of the default Python interpreter in it:source my27project/bin/activatepython --version# This will show Python 2.7.13deactivate# Activate the my36project sandbox and check the version of the default Python interpreter in it:source my36project/bin/activatepython --version# This will show Python 3.6.0deactivate

参考文档

  1. How to install the latest version of Python on CentOS
  2. make install和make altinstall 的区别的问题
0 0
原创粉丝点击