为了体验python 3.7,安装了最新的Anaconda环境( Anaconda3-2018.12-Linux-x86_64.sh ),但工作上编译Android ROM又得需要python 2.7,来回切换很麻烦。探索到了virtualenv这个工具,可以在不切换系统默认python版本的情况下创建一个python 2.7环境。
首先,确认系统默认python为3.7版本:
$ python -V
Python 3.7.1
$ which python
/opt/anaconda3/bin/python
然后,安装virtualenv和virtualenvwrapper:
$ pip install virtualenv
$ pip install virtualenvwrapper
然后,将如下内容添加到~/.bashrc的末尾:
if [ -f /opt/anaconda3/bin/virtualenvwrapper.sh ]; then
export WORKON_HOME=$HOME/.virtualenvs
source /opt/anaconda3/bin/virtualenvwrapper.sh
fi
然后,创建python 2.7环境:
$ mkvirtualenv -p /usr/bin/python --no-site-packages python2_env
最后,进入python2_env环境,检查python版本:
~$ workon python2_env
(python2_env) ~$ python -V
Python 2.7.12
如果要退出这个python2.7环境,输入deactivate命令即可。