安装python3.6.5和python虚拟化环境

准备环境

  • 操作系统:CentOS 7.6 64位

1、安装依赖包

[root@localhost ~]# yum install -y wget vim lrzsz zip unzip telnet gcc-c++ tcl​

2、安装python3.6.5

[root@localhost ~]# cd /data/src/
[root@localhost src]# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
[root@localhost src]# tar -xf Python-3.6.5.tgz
[root@localhost src]# mv Python-3.6.5 ../soft/ 
[root@localhost src]# cd ../soft/Python-3.6.5
[root@localhost Python-3.6.5]# mkdir -p /data/soft/python3
[root@localhost Python-3.6.5]# ./configure --enable-optimizations --prefix=/data/soft/python3
[root@localhost Python-3.6.5]# make && make install
[root@localhost Python-3.6.5]# cd /data/soft/python3/bin
[root@localhost bin]# python3
Python 3.6.7 (default, Jul 13 2019, 16:46:47) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@localhost bin]# mkdir -p /root/.pip
[root@localhost bin]# vim /root/.pip/pip.conf
## Note, this file is written by cloud-init on first boot of an instance
## modifications made here will not survive a re-bundle.
###
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com
[root@localhost bin]# ln -s /data/soft/python3/bin/python3 /usr/local/bin/python3

3、安装虚拟化环境

[root@localhost bin]# pip3 install virtualenvwrapper
[root@localhost bin]# mkdir /root/Envs
[root@localhost bin]# vim /root/.bashrc
……
#新增python配置
export WORKON_HOME=~/Envs
source /usr/local/bin/virtualenvwrapper.sh
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv

[root@localhost bin]# cd 
[root@localhost ~]# . /root/.bashrc
[root@localhost ~]# virtualenvwrapper –help

4、创建虚拟化环境

[root@localhost ~]# mkvirtualenv cmdb_frontend
Using base prefix '/usr/local'
New python executable in /root/Envs/cmdb_frontend/bin/python3.6
Also creating executable in /root/Envs/cmdb_frontend/bin/python
Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /root/Envs/cmdb_frontend/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/Envs/cmdb_frontend/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/Envs/cmdb_frontend/bin/preactivate
virtualenvwrapper.user_scripts creating /root/Envs/cmdb_frontend/bin/postactivate
virtualenvwrapper.user_scripts creating /root/Envs/cmdb_frontend/bin/get_env_details
(cmdb_frontend) [root@localhost ~]# deactivate 
[root@localhost ~]#

5、在虚拟化环境下,运行cmdb-frontend

[root@localhost ~]# cd /data/app/cmdb/cmdb-frontend
[root@localhost cmdb-frontend]# workon cmdb_frontend
(cmdb_frontend) [root@localhost cmdb-frontend]# pip install -r requirements.txt 
(cmdb_frontend) [root@localhost cmdb-frontend]#

6、附录-常用命令:

#查看虚拟环境列表:
workon
#进入虚拟环境:
workon cmdb_frontend
#退出虚拟环境:
deactivate
#查看python依赖包清单:
pip list
#导出依赖包清单
pip freeze > requirements.txt
#导入依赖包清单
pip install -r requirements.txt

你可能感兴趣的:(DevOps,python)