python虚拟环境安装
一、环境要求
系统版本:CentOS 7.3(自带Python2.7)
安装版本:Python3.6
安装插件:virtualenv、virtualenvwrapper
1.安装依赖环境
[root@vm10-0-1-23 ~]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
2.下载python包
# 大家可以根据自己的需求下载指定的python版本 [root@vm10-0-1-23 ~]# wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
3.编译安装python包
# 创建python目录 [root@vm10-0-1-23 ~]# mkdir /usr/local/python3 # 解压压缩包,进入解压目录,指定安装目录,安装Python3 [root@vm10-0-1-23 ~]# cd /root/Python-3.6.6 [root@vm10-0-1-23 Python-3.6.6 ]# ./configure --prefix=/usr/local/python3 [root@vm10-0-1-23 Python-3.6.6 ]# make && make install # 创建软连接 [root@vm10-0-1-23 Python-3.6.6 ]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3 [root@vm10-0-1-23 Python-3.6.6 ]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 [root@vm10-0-1-23 Python-3.6.6 ]# ln -s /usr/local/python3/bin/virtualenv /usr/local/bin/virtualenv
二、虚拟环境安装
virtualenv是一个可以在同一计算机中隔离多个python版本的工具。有时,两个不同的项目可能需要不同版本的python,如 python2.7 / python3.6 ,但是如果都装到一起,经常会导致问题。virtualenv能够用于创建独立的Python虚拟环境,多个Python相互独立,互不影响。
virtualenvwrapper这个软件包可以让我们管理虚拟环境变得更加简单。不用再跑到某个目录下通过virtualenv来创建虚拟环境,并且激活的时候也要跑到具体的目录下去激活。
1. 安装virtualenv、virtualenvwrapper
[root@vm10-0-1-23 ~]# pip3 install virtualenv [root@vm10-0-1-23 ~]# pip3 install virtualenvwrapper
2. 进入.bashrc文件中,定义virtualenvwrapper路径
[root@vm10-0-1-23 ~]# vim ~/.bashrc VIRTUALENVWRAPPER_PYTHON=/usr/local/python3/bin/python3 # 指定virtualenvwrapper执行的python版本 export WORKON_HOME=$HOME/.virtualenvs # 指定虚拟环境存放目录,.virtualenvs目录名可自拟 source /usr/local/bin/virtualenvwrapper.sh # virtualenvwrapper.sh所在目录 # source生效配置 [root@vm10-0-1-23 ~]# source ~/.bashrc
至此python虚拟环境就安装完成,下面是虚拟环境中常用命令
# 创建虚拟环境 [root@vm10-0-1-23 ~]# mkvirtualenv py3-env # 指定python版本 [root@vm10-0-1-23 ~]# mkvirtualenv --python=/usr/bin/python3 py3-env # 切换到某个虚拟环境 [root@vm10-0-1-23 ~]# workon my_env # 退出当前虚拟环境 [root@vm10-0-1-23 ~]# deactivate # 删除某个虚拟环境 [root@vm10-0-1-23 ~]# rmvirtualenv my_env # 列出所有虚拟环境 [root@vm10-0-1-23 ~]# lsvirtualenv # 进入到虚拟环境所在的目录 [root@vm10-0-1-23 ~]# cdvirtualenv
最后提供了脚本一键安装虚拟环境
[root@vm10-0-1-23 ~]# cat install.sh #!/bin/bash # 安装python3环境 PYTHON_INSTALL() { version="3.6.6" package="Python-3.6.6.tar.xz" url="https://www.python.org/ftp/python/{$version}/{$package}" yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel wget url if [ ! -d /usr/local/python3 ];then rm -rf /usr/local/python3 && mkdir -p /usr/local/python3 tar -xf /root/$package ./configure --prefix=/usr/local/python3 && make && make install ln -s /usr/local/python3/bin/python3 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 ln -s /usr/local/python3/bin/virtualenv /usr/local/bin/virtualenv echo "Python environment installed successfully" fi v=`python3 -V |awk '{print $2}'` if [ $v == $version ];then pip3 install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple pip3 install virtualenvwrapper -i https://pypi.tuna.tsinghua.edu.cn/simple cat >>~/.bashrc<<EOF VIRTUALENVWRAPPER_PYTHON=/usr/local/python3/bin/python3 export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh EOF soure ~/.bashrc echo "The virtual environment was installed successfully" fi } PYTHON_INSTALL