环境
系统:CentOS7 1804 x64
安装Python3.7.6
安装依赖及工具
yum update
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
yum install -y libffi-devel
yum install -y vim*
上传python压缩包到 /root/,并编译安装
tar -zxvf Python-3.7.6.tgz
cp -rf Python-3.7.6 /usr/bin/
cd /usr/bin/Python-3.7.6/
./configure prefix=/usr/local/python3
make && make install
mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/python3/bin/python3 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
修改系统必要配置文件为python2
修改以下文件的开头python为python2
vim /usr/bin/yum
vim /usr/libexec/urlgrabber-ext-down
配置 pip
设置 pip 默认源 为 豆瓣源
mkdir ~/.pip
mkdir $HOME/.pip
vim $HOME/.pip/pip.conf
在pip.conf中写入:
[global]
timeout = 60
index-url = https://pypi.doubanio.com/simple
trusted-host = pypi.douban.com
升级pip
pip install --upgrade pip
pip 升级 不修改默认源 国内 使用豆瓣源
pip install --upgrade pip -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
不修改默认源,安装包
pip install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com 包名
安装配置virtualenv和virtualenvwrapper
pip install virtualenv
pip install virtualenvwrapper
mkdir $HOME/.virtualenvs
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
不进行软链则报错:
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
find / -name virtualenvwrapper.sh
复制找到的路径,如下:
/usr/local/python3/bin/virtualenvwrapper.sh
vim $HOME/.bashrc
追加
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/python3/bin/virtualenvwrapper.sh
source $HOME/.bashrc
相关命令:
新建虚拟环境
mkvirtualenv 虚拟环境名称
列出虚拟环境列表
workon 或 lsvirtualenv
启动/切换虚拟环境
workon 虚拟环境名称
删除虚拟环境
rmvirtualenv 虚拟环境名称
退出虚拟环境
deactivate