What to do when anaconda’s deactivate and virtualenvwrapper’s deactivate conflict

Daniel Gitu
Jul 24, 2017 · 2 min read

Over the weekend I attended a meetup on data science which was to teach us on extracting data from the genome. It was a great introduction to the topic.

One of the requirements was that we had to install anaconda, a Python and R distribution for large scale data processing. [1]

The installation process was easy enough and the talk went great. I wake up Monday morning to activate my virtualenv to find

Error: deactivate must be sourced. Run 'source deactivate'
instead of 'deactivate'.

My virtual environment was active but this was disturbing. I had to investigate.

During my investigation I came to learn that: [2]

activate is an executable script in anaconda’s bin directory, but is a function in virtualenvwrapper.sh. So this is sort of a namespace collision problem, but also a case of overlap in functionality.

To solve this: [3]

So, in the virtualenvwrapper.sh script, we can change the following two lines which test for whether deactivate is merely callable:

type deactivate >/dev/null 2>&1
if [ $? -eq 0 ]

with the stricter test for whether it’s a shell function:

nametype="$(type -t deactivate)"
if [ "${nametype##* }" == "function" ]

This change avoids triggering the spurious error noted in the original question, but doesn’t risk redirecting other useful errors or output into silent oblivion.

If you installed virtualenvwrapper using pip, you should find it in the directory

$ cd /usr/local/bin

If your installation is similar to mine, you should find it in the directory

$ cd ~/.local/bin

If you use vim as your editor open the file

$ vim virtualenvwrapper.sh

While still in the normal mode [4], type

:? type deactivate

This will take you directly to the lines you intend to change

type deactivate >/dev/null 2>&1
if [ $? -eq 0 ]

You can delete them or comment them out and add the following two lines

nametype="$(type -t deactivate)"
if [ "${nametype##* }" == "function" ]

Exit the script and source the file for your changes to take effect.

:wq

Then

source virtualenvwrapper.sh

That’s all folks.

[1] — https://stackoverflow.com/a/42096429, https://en.wikipedia.org/wiki/Anaconda_(Python_distribution)

[2] — https://stackoverflow.com/a/38198815

[3] — https://stackoverflow.com/a/42014049

[4] — https://en.wikibooks.org/wiki/Learning_the_vi_Editor/Vim/Modes

I am a neophyte. I like challenging myself and learning new things. Currently learning python, among other things

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade