virtualenvwrapper commands aren't working

tow-81-235:Projects pessimisticoptimism$ mkvirtualenv development
-bash: mkvirtualenv: command not found
tow-81-235:Projects pessimisticoptimism$ sudo pip install virtualenvwrapper
Password:
Requirement already satisfied (use --upgrade to upgrade): virtualenvwrapper in /Library/Python/2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Python/2.7/site-packages (from virtualenvwrapper)
Requirement already satisfied (use --upgrade to upgrade): virtualenv-clone in /Library/Python/2.7/site-packages (from virtualenvwrapper)
Cleaning up...
tow-81-235:Projects pessimisticoptimism$ mkvirtualenv development
-bash: mkvirtualenv: command not found

Why am I getting this error? I have virtualenv and virtualenvwrapper installed. I'd like to use mkvirtualenv and workon. I find it odd that virtualenv is working, but virtualenvwrapper isn't.


Solution 1:

1st, ensure you're installing with sudo:

sudo pip install virtualenvwrapper

2nd, append the following lines to your .bashrc file (with nano ~/.bashrc):

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

3rd, reload your profile

source ~/.bashrc

Solution 2:

Summary

I'm on a Mac and my answer is similar to @Ramces answer except it was with bash_profile. I just want to elaborate a little further for Mac users to be aware that there's a lot of different profiles including:

  • .bashrc
  • .bash_profile
  • .profile

Some files like .profile do not take precedence over .bash_profile (if it exists) and will then be ignored. If you successfully do the below steps and get a virtual env working, but then close out your terminal and 'workon command not found', then you need to setup for the correct profile. For a detailed answer, see here

Install Steps:

  1. sudo pip install virtualenv

    Installs virtualenv (allows you to separate your envrionments)

  2. sudo pip install virtualenvwrapper

    Installs virtualenvwrapper (allows you to use the 'workon' command)

  3. nano ~/.bash_profile

    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/virtualenvwrapper.sh
    
  4. source ~/.bash_profile

    Reloads the profile. Going forward you only need step 5 (to create new environments) and step 6 (to run environments)

  5. mkvirtualenv my_env

    This creates your virtual environment (this example is with 'my_env')

  6. workon my_env

    This lets you work on a specific environment (this example is with 'my_env')

Solution 3:

After installing the virtualenvwrapper package using pip, you also have to do some initialisation/set your preferences. See the introduction in the virtualenvwrapper docs.

Most relevant for finding the commands should be sourcing the virtualenvwrapper script into your shell. In the docs it is mentioned as

$ source /usr/local/bin/virtualenvwrapper.sh

You still have to adjust the path to your setup. My guess for your Mac would be:

$ source /Library/Python/2.7/site-packages/virtualenvwrapper.sh

Solution 4:

Simple process

  1. sudo apt-get install python-pip(if pip is not installed)
  2. sudo pip install virtualenv
  3. Create a dir to store your virtualenvs

    mkdir ~/.virtualenvs

  4. sudo pip install virtualenvwrapper

  5. Run following command

    export WORKON_HOME=~/.virtualenvs

  6. Add virtualenvwrapper.sh to .bashrc Add this line to the end of ~/.bashrc so that the virtualenvwrapper commands are loaded.

    . /usr/local/bin/virtualenvwrapper.sh

  7. you will find .bashrc.sh file in home directory by doing ctrl+h. if not then use find command to find .bashrc.sh "file ls -la ~/ | more"

  8. Hit this command

    source /usr/local/bin/virtualenvwrapper.sh

  9. Hit this command

    source ~/.bashrc

Solution 5:

It sounds like you have multiple Python installations on your machine and virtualenvwrapper is not pointing to the right Python.

Find out which Python virtualenvwrapper is using. You get a hint where to look with which virtualenvwrapper.sh (In this case /usr/local/bin):

> /usr/local/bin/virtualenvwrapper.sh

If you don't get any return here make sure you use the correct pip when installing. The pip command might link to a different Python then you expect. Check your usr/local/bin directory for pip links (pip, pip2, pip2.7, pip3, pip3.5). It is easy to get system pip, pip2 and pip2.7 mixed up.

After you have found the Python location, add/update all paths in your .profile:

export WORKON_HOME=$HOME/venv
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2
source /usr/local/bin/virtualenvwrapper.sh

Finally reload your profile: source ~/.profile