centos7安装python3.6独立的virtualenv环境
1.编译安装python3.6环境
# 安装依赖
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
# 获取编译安装python3.6.6
mkdir -p /usr/local/python3
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
tar xf Python-3.6.6.tgz
cd Python-3.6.6
./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
2.安装virtualenv虚拟环境
pip3 install virtualenv
# 创建存放虚拟环境的目录
mkdir -p /usr/local/worksh/jeninks_task/akamai_api_venv
# 创建纯净的虚拟环境
/usr/local/python3/bin/virtualenv --no-site-packages --python=/usr/local/python3/bin/python3.6 /usr/local/worksh/jeninks_task/akamai_api_venv
# 进入虚拟环境安装必须的python依赖
[root@node5 akamai_api]# source ../akamai_api_venv/bin/activate
(akamai_api_venv) [root@node5 akamai_api]# pip install -r requirement.txt
(akamai_api_venv) [root@node5 akamai_api]# pip list
Package Version
--------------- ---------
asn1crypto 0.24.0
certifi 2019.6.16
cffi 1.12.3
chardet 3.0.4
configparser 3.7.4
cryptography 2.7
edgegrid-python 1.1.1
idna 2.8
ndg-httpsclient 0.5.1
pip 19.2.2
pyasn1 0.4.6
pycparser 2.19
pyOpenSSL 19.0.0
requests 2.22.0
setuptools 41.0.1
six 1.12.0
urllib3 1.25.3
wheel 0.33.4
# 后续可以直接这样调用独立环境的脚本
[root@node5 akamai_api]# /usr/local/worksh/jeninks_task/akamai_api_venv/bin/python /usr/local/worksh/jeninks_task/akamai_api/aa.py
centos7环境编译安装基于 python3.7.7 的virtualenv和virtualenvwrapper环境
1. 安装基本的基础源和epel源为阿里云的mirrors
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
2.安装依赖
# bzip2-devel 一定要先安装,然后编译python,compress插件依赖
yum install -y gcc gcc-c++ htop telnet iotop iptraf iftop libpng12 make logrotate xinetd ntsysv sysstat perl autoconf libjpeg libjpeg-devel libpng libpng-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel libxml2 libxml2-devel libxslt-devel libevent-devel libtool libtool-ltdl bison ntpdate patch vim wget openssh-clients bind-utils
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 mysql-devel python-devel mysql-lib
yum install libffi-devel -y
3.安装python3.7,最好开screen避免中断
screen -S python
# 获取python3.7.7源码并编译安装
cd /usr/local/src
wget https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tgz
tar xf Python-3.7.7.tgz
cd Python-3.7.7
mkdir -p /usr/local/python3
./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
[root@sz_xxgc_cms_slave01_18_214 Python-3.7.7]# python3 -V
Python 3.7.7
4.安装虚拟环境
# pip3 install virtualenv
# pip3 install virtualenvwrpper
5. 配置bashrc
# vim ~/.bashrc
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/python3/bin/virtualenvwrapper.sh
# source ~/.bashrc # 使其生效
[root@sz_xxgc_cms_slave01_18_214 ~]# mkvirtualenv domain_check
which: no virtualenv in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
ERROR: virtualenvwrapper could not find virtualenv in your path
解决:
将python环境下的virtualenv拷贝至/usr/bin/目录下:
# cd /usr/local/python3/bin
# cp virtualenv /usr/bin/
[root@sz_xxgc_cms_slave01_18_214 bin]# mkvirtualenv domain_check
created virtual environment CPython3.7.7.final.0-64 in 1225ms
creator CPython3Posix(dest=/root/.virtualenvs/domain_check, clear=False, global=False)
seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1)
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
virtualenvwrapper.user_scripts creating /root/.virtualenvs/domain_check/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/domain_check/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/domain_check/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/domain_check/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/domain_check/bin/get_env_details
(domain_check) [root@sz_xxgc_cms_slave01_18_214 bin]# workon domain_check
(domain_check) [root@sz_xxgc_cms_slave01_18_214 bin]# pip list