Simple Python Version Management: pyenv

Pyenv makes using different versions of Python just a bit easier.

Pyenv makes using different versions of Python just a bit easier.  If you've worked with any machine learning or crypto [blockchain] modules, you will notice that some things work fine on Python 3.6 and other Python 3.7 but some may not install on both as the project's have not updated. Or you may be stuck on a certain version due to company requirements.

Now you can have your cake and eat it.

External links:

Quick install, and usage.

Install on OSX

$ brew update
$ brew install pyenv

# Might be needed
$ brew install readline xz

Run the command below to add to bash profile

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

Restart the terminal app

Pyenv Useful Commands

# Show the current python version
pyenv version

# Show the available python versions
pyenv install -l

# Install Python 3.6.4
pyenv install 3.6.4

# Install Python 3.7.4
pyenv install 3.7.4

# Show the current python versions installed on your system
pyenv versions

# Set the global python version to 3.7.4 
pyenv global 3.7.4

# Set to the original system python [macOS Mojave - Python 2.7.16]
pyenv global system

# Set the local directory to Python 2.7.16
# [This command will create the hidden file .python-version]
pyenv local 2.7.16

Working with virtualenvs

Since we are overriding with pyenv, we need to shim some things for virtualenvs to work properly. Luckily they created it and all you need to do is install pyenv-virtualenv.

Install pyenv-virtualenv

brew install pyenv-virtualenv
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

# Probably a good idea to restart your terminal.

Create a virtualenv

pyenv virtualenv 2.7.16 my-virtual-env-2.7.16

List virtualenvs

pyenv virtualenvs

Activate a virtualenv

pyenv activate <name>

# Example from above
pyenv activate my-virtual-env-2.7.16

Deactivate a virtualenv

pyenv deactivate

Remove a virtualenv

pyenv uninstall <name>

# Example from above
pyenv uninstall my-virtual-env-2.7.16

A nice project workflow

# Create a new project directory
cd current_projects
mkdir new_project
cd new_project

# Set the local python version to what ever version is required [example: 3.6.4]
pyenv local 3.6.4

# Create a virtualenv for the new project
pyenv virtualenv new_project_3.6.4

# Activate the project
pyenv activate new_project_3.6.4

# Do Work, save the planet with Python, etc.

PyCharm Integration

Due to our setup above we will need to update our usual PyCharm workflow. Normally when you create a project directory and set the virtualenv inside, PyCharm will pick it up and automatically set itself up for working within that project and virtualenv.  We just need to force it to the right virtualenv directory.

For example in the screenshot below I am opening a test project and setting the interpreter to the pyenv virtualenv's directory.  


note: If you've worked within PyCharm for awhile with various virtualenv's this will make sense.

I hope this works out for you and makes you Python development easier.