- 需求
当公司的项目使用python构建,并且使用的是python3.x版本时,使用该方法,能够实现环境隔离。
1. 安装需要的python版本
下载地址:https://www.python.org/ftp/python/
```bash
[root@python local]$ wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz
[root@python local]$ tar xf Python-3.7.6.tar.xz && cd Python-3.7.6
[root@python local]# ./configure --prefix=/usr/local/python3
有关虚拟环境的报错,修改 Setup.dist
[root@python local]$ cd Python-3.7.6/Modules
[root@python Modules]$ vim Setup.dist
_socket socketmodule.c
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
编译安装
[root@python Python-3.7.6]# yum -y groupinstall "Development tools"
[root@python Python-3.7.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
[root@python Python-3.7.6]# yum install libffi-devel -y
[root@python ~]# cd /usr/local/Python-3.7.6
[root@python Python-3.7.6]# make && make install
文件最后添加
[root@python Python-3.7.6]# vim /etc/profile
export PATH=$PATH:/usr/local/python3/bin
[root@python Python-3.7.6]# source /etc/profile
2. 安装虚拟环境
[root@python test]# pip3 install virtualenv
[root@python test]# virtualenv venv
[root@python test]$ source venv/bin/activate
(venv) [ops@python test]$ deactivate
3. pip安装比较慢,需要修改一下源站即可
pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com