26

What happened:

After an OSx update and installing a new version of python 2.7 my virtualevn environment completely broke and I struggled in fixing it. I wasn't sure what caused it and went through a whole set of things that I did and read initially that didn't work are listed below. What solved my problem is provided in the answer section.

What didn't work to fix virtualenv command not found:

  • Installed python through homebrew and then used pip to install virtualenv
  • Installed python through https://www.python.org and then used pip to install virtualenv

Related questions that helped me but did not provide the solution to my problem:

  1. virtualenv-command-not-found
  2. virtualenv-workon-command-not-found

Complete manual recovery I went through (What not to do!):

This didn't completely solved my problem. It is just to give you an idea of what steps I went through before I found the correct way to fix my python dev environment on my OSx.

  • Removed python 2.7 by using the post in here
  • Removed the homebrew installed version
  • Installed python through the pkg file in Mac OS X 32-bit i386/PPC installer or Mac OS X 64-bit/32-bit installer
  • Manually installed virtualenv following the instructions from here:

     curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-13.1.2.tar.gz
     tar xvfz virtualenv-13.1.2.tar.gz 
     cd virtualenv-13.1.2 
     sudo python setup.py install
    
  • Manaully install pip through 7: curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python2.7

PIP was still broken after all this:

After all this after creating a virtual environment my pip still installed the packages in the main python folder instead of installing them under the virtual environment and non of the threads here neither here helped. My solution to that was to run pip under my virtual env with the following options: 1- Activate the virtual environment so that $VIRTUAL_ENV is set:

source venv/bin/activate

2- Forces pip to install in the right destination:

pip install --target=$VIRTUAL_ENV/lib/python2.7/site-packages   

Summary

Something was badly broken and best way I fix my dev environment is provided in the answer to this question.

AmirHd
  • 10,308
  • 11
  • 41
  • 60
  • I just had this issue after restoring from Time Machine and upgrading to the latest version of Sierra. Anything that used python resulted in the error `python --version Illegal instruction: 4`, after reinstalling it seems to be working again. – doublesharp Oct 28 '16 at 18:40
  • find a solution here https://stackoverflow.com/questions/25372911/python-pip-error-on-osx/47832840#47832840 – Sébastien Allamand Dec 15 '17 at 12:57

2 Answers2

50

The reason

In my case was an OSx upgrade that affected my homebrew and after upgrading to python 2.7.11 is didn't install it properly.

How I got it to work:

I found steps 3 and 4 in a thread here and many thanks to https://github.com/baronomasia.

1 - Removed python 2.7 by using the post in here

2 - Removed the homebrew python installed version

brew uninstall python

3- Reinstall your Xcode command tools:

sudo xcode-select --install

4- Upgrade homebrew and reinstall python through homebrew:

brew update && brew reinstall python    
Community
  • 1
  • 1
AmirHd
  • 10,308
  • 11
  • 41
  • 60
  • 5
    For the step 2, I had to add --force flag. i.e. brew uninstall python --force – dhfromkorea Dec 18 '16 at 04:37
  • I tried this trying to fix my "Command "python setup.py egg_info" failed with error code 1" error when-ever i run pip2 to but it doesn't seem to have worked. don't know if anyone else is still getting this broken pip issue? – Ben Aug 13 '17 at 23:05
  • 1
    Also, to remove the symlinks you can use this command: `cd /usr/local/bin/` then, `ls -l /usr/local/bin | grep '../Library/Frameworks/Python.framework/Versions/2.7' | awk '{print $9}' | tr -d @ | xargs rm` – Shubham Kushwah Jul 02 '18 at 17:38
3

After doing brew upgrade python my system python was broken and was throwing fits about virtualenvwrapper.sh, as well as my pip command was just suddenly missing. I went to python.org and downloaded the python 2.7.13 installer, ran it, I now have python 2.7.13, pip, and can run pip install virtualenvwrapper and things seem to work.

nackjicholson
  • 4,557
  • 4
  • 37
  • 35