Centos7搭建python及运行虚拟环境

Centos7搭建python及运行虚拟环境

    • 1搭建python3环境
    • 2搭建python3虚拟环境

1搭建python3环境

1.安装相应的编译工具
在root用户下(不要用普通用户,麻烦),全部复制粘贴过去,一次性安装即可.

yum -y groupinstall "Development tools"
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
yum install -y libffi-devel zlib1g-dev
yum install zlib* -y

2.下载安装包

wget wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz

3.解压

tar -xvJf  Python-3.7.2.tar.xz

4.创建编译安装目录

mkdir /usr/local/python3 

5.安装

cd Python-3.7.2
./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl 
#第一个指定安装的路径,不指定的话,安装过程中可能软件所需要的文件复制到其他不同目录,删除软件很不方便,复制软件也不方便.
#第二个可以提高python10%-20%代码运行速度.
#第三个是为了安装pip需要用到ssl,后面报错会有提到.
make && make install

6.创建软链接

ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3

7.验证是否成功

python3 -V
pip3 -V

2搭建python3虚拟环境

1安装virtualenv,virtualenvwrapper

sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper 

2创建虚拟环境的工作目录

 mkdir /root/.virtualenvs
 ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv

3配置环境变量

vim ~/.bashrc

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
export WORKON_HOME=/root/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/python3/bin/virtualenvwrapper.sh

4使环境变量生效

source /root/.bashrc

5创建一个虚拟环境

 mkvirtualenv daohan -p /usr/bin/python3 #指定python版本
 mkvirtualenv daohan #普通版本
 # 退出虚拟环境
deactivate

你可能感兴趣的:(python)