pyenv and pyenv-virtualenv - Intro

Page content

Installing stacks

You may be confused at first the differences between,

  • pyenv
  • virtualenv
  • pyenv-virtualenv

Especially, virtualenv sounds like Linux virtual environment, but it isn’t at all. Here is the good answer about that.

Conclusion: pyenv-virtualenv is the best choice.

pyenv-virtualenv official: https://github.com/pyenv/pyenv-virtualenv

Install pyenv

Install pyenv in macOS (or other Linux environments).

# Download source under ~/.pyenv
git clone https://github.com/yyuu/pyenv.git ~/.pyenv

# Set PATH and another variable
echo -e '\n
export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"
' \
>> ~/.zshrc

In the case of WSL, .bashrc instead .zshrc.

Install pyenv-virtualenv

git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc
source ~/.zshrc

In case of WSL and PoP!OS 20.10, install the followings.

sudo apt install make zlib1g zlib1g-dev bzip2 libbz2-dev libreadline8 libreadline-dev libssl-dev libsqlite3-dev tk-dev liblzma-dev

Pyenv - how to

Check the available Python version

pyenv install --list

Install a Python version

Following command install Python 3.8.1 environment.

pyenv install 3.8.1
pyenv rehash

This environment would be the base of your own Python environment.

Create an environment

I made an environment which name is nlp and its Python version is 3.8.1.

pyenv virtualenv 3.8.1 nlp
pyenv rehash

Check environments

pyenv versions
* system (set by /Users/atlex/.pyenv/version)
  3.8.1
  3.8.1/envs/nlp

In this sample, there are 3 environment, and I’m using system environment.

Activate the environment

Now I activate the environment I maed.

pyenv activate nlp

And feel free to use your Python environment!

Out from environment

After you’ve done your work, log out from your environment if you need.

pyenv deactivate

Delete pyenv environment

If you have failed to build your environment, following command delete your environment.

pyenv uninstall nlp

Update pyenv

https://stackoverflow.com/questions/43993714/why-is-python-3-6-1-not-available-in-pyenv

cd $(pyenv root) && git pull