Learning by doing.
Multiversion Python with PyENV
Altough I am not a fan of Python 2, as a PhD student in science, I have to deal with Python 2 code sometime, so a tool is needed to manage the multiversion Python, and PyENV is the tool.
Step 1. Install PyENV via Homebrew
$ brew update $ brew install pyenv $ brew install --HEAD pyenv-virtualenv
Then add the entry below into the
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
Step 2. Setup different versions of Python
First, check the list of the versions of Python that can be installed:
$ pyenv install -l
What I’d like to install are the Anaconda release version of Python 2 and 3, hence
$ pyenv install anaconda-4.0.0 $ pyenv install anaconda3-4.0.0
Step 3. Switch between versions
We can simply switch between versions by:
$ pyenv global/local <version>
local, the version is used only under the directory; with
global, the version is used globally.
After that, we need to
$ pyenv rehash
Or, we can switch between environments by setting virtual environments:
$ pyenv virtualenv anaconda-4.0.0 py2 # create the environment called py2 with anaconda-4.0.0 $ pyenv virtualenv anaconda3-4.0.0 py3 # create the environment called py3 with anaconda-4.0.0 $ pyenv activate py2 # activate the environment $ pyenv deactivate # return to system environment $ pyenv activate py3 # activate the py3 environment $ rm -rf ~/.pyenv/versions/py2/ # delete the py2 environment
When the download speed is too slow, we can download the install-package of Python with other tools and make a local-hosted image of the packages.
For example, if we have downloaded the Anaconda package under
$ cd ~/Downloads && python3 -m http.server
which returns a URL, say,
$ export PYTHON_BUILD_MIRROR_URL="http://0.0.0.0:8000/" $ pyenv install anaconda-4.0.0
and we will find that the file is still being downloaded from the original URL instead of the one we just set. Don’t worry, check out the message of the python http server, we find a HEAD which contains a 64-bit string. Rename the package we downloaded with the string:
$ mv Anaconda2-4.0.0-MacOSX-x86_64.sh a3443b46f99bc6680c77c688af1b1f5a
Then install again:
$ pyenv install anaconda-4.0.0
and it will install the packaged directly with the file we downloaded.