-
Install pyenv and pyenv-virtualenv
brew install pyenv brew install pyenv-virtualenv
-
Install different versions of python
pyenv install 3.6.4 pyenv install 3.6.0
-
Install direnv
brew install direnv
-
Update
~/.zshrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc echo 'eval "$(pyenv init -)"' >> ~/.zshrc echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc echo 'eval "$(direnv hook zsh)"' >> ~/.zshrc
-
Restart shell
-
Add
.direnvrc
under~/
-
Create a project directory.
mkdir project_1
-
Create virtual environment:
pyenv virtualenv 3.6.7 venv-name
-
Create
.python-version
under./project_1
with the virtual environment name created in step 2.venv-name
-
Create
.envrc
under./project_1
with environment variables declaration like:export ENV_VARIBALE_NAME=bla
-
Enter the project directory, run this command to trust current environment, you only need to run this at this first time and once
.envrc
is updated.cd projects_1/ direnv allow
Now anytime you enter ./project_1/
or a directory under it, the virtualenv will automatically be activated, and environment variables will be setup.
- For IDE virtualenv setup, the created virtualenv is under directory
~/.pyenv/versions/virtualenv_name/