Python development environment setup.

Install pyenv

Macos comes with Python 3.8 and changing the system python version is not recommended so we use pyenv to be able to use other python versions without modifing the system.
brew install pyenv
Setup environment variables. In zsh it will be in ~/.zshrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

Some pyenv useful commands

Install a python version in the machine
pyenv install 3.9.1
pyenv install 3.5.10
...
Set a particular python version globally (will be the default when using python command). Internally it writes 3.9.1 into file ~/.pyenv/version. This file will be read by pyenv init - command when starting the shell.
pyenv global 3.9.1
Set a particular python version locally. This will create a .pyenv/version in current directory.
pyenv local 3.5.10

Install virtualenvwrapper

Different projects have different dependencies and each project should be isolated. virtualenv is a tool that creates isolated environments (in a directory) and the wrapper is as the name suggests a wrapper that helps centralizing this environments so we don't end up with lots of lots of boiler plate environment files in each project. I find it funny that virtualenvwrapper also depends on python. I have to assume that it uses functions that are stable and do not vary depending on different version of python.
pip install virtualenvwrapper
As anything installed via pip it can be uninstalled :
pip uninstall virtualenvwrapper
To be able to use functions of virtualenvwrapper we need to execute virtualenvwrapper.sh or virtualenvwrapper_lazy.sh first. So I add this: Then setup environment variables. In zsh it will be in ~/.zshrc
# Run `pip uninstall virtualenvwrapper` to find out real location of virtualenvwrapper
# This might take varous seconds so disable this when not using python
if [ -f .pyenv/versions/3.9.1/bin/virtualenvwrapper_lazy.sh ]; then
     export WORKON_HOME=$HOME/.pythonvirtualenvs # Optional
     # export PROJECT_HOME=$HOME/projects      # Optional
     export VIRTUALENVWRAPPER_PYTHON=$(which python) #Optional
     .pyenv/versions/3.9.1/bin/virtualenvwrapper_lazy.sh
fi
From official docs:
When the lazy-loading version of the startup script is used, tab-completion of arguments to virtualenvwrapper commands (such as environment names) is not enabled until after the first command has been run. For example, tab completion of environments does not work for the first instance of workon.
Now some commands will he available:
  • workon
  • deactivate
  • mkvirtualenv
  • cdvirtualenv
  • rmvirtualenv

0 comments :

This work is licensed under BSD Zero Clause License | nacho4d ®