For Python app developers, its a common practice to use virtual environments. Virtualenvwrapper
is a tool to conveniently work with virtual environments. As the name implies, it is a wrapper over virtualenv
. The following setup has been tested on MacOS Catalina however it should also work on other versions with a little or no modifications.
Setup time (approx.) 30 minutes.
Steps
- Install Homebrew, if you don’t have it already.
There are other possibilities as well to install Python3 but I recommend installing it via Homebrew for cleaner installation and easier maintenance (upgrade etc).
Command:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
- Install Python3
Command:
brew install python3
pip3 also gets installed automatically with above command. - Install Virtualenvwrapper
Command:
sudo -H pip3 install virtualenvwrapper
- In .zshrc (or bashrc), add following lines:
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
In your home directory, you’ll find one of the two;.zshrc
or.bashrc
depending on the shell you use. If there is no such file, please create one (again according to the shell you use).
Please note that Mac Catalina uses Zsh by default. - Load the new configuration
Command:
source ~/.zshrc
orsource ~/.bashrc
- Create a virtual environment
You should be able to use following command without error:
Command:mkvirtualenv test-env
test-env should have been created and activated.
PS: You can remove the test-env, if you don’t want to keep it, with command:rmvirtualenv test-env
. - Activate a virtual environment / Switch between virtual environments
Command:
workon <name-of-env>
If you havetest-env-1
andtest-env-2
created, you can switch between these two usingworkon
command. - Deactivate the current virtual environment
Command:
deactivate
- Remove a virtual environment
Command
rmvirtualenv <name-of-env>