When I run virtualenv, I get this:
$ virtualenv
-bash: /usr/local/bin/virtualenv: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory
virtualenv only started behaving this way today. It worked yesterday. It breaks because virtualenv is a Python script using a nonexistent Python interpreter:
$ head -1 $(which virtualenv)
#!/usr/local/opt/python/bin/python2.7
On my machine, /usr/local/opt/python is a symlink to a Python 3.6 directory:
$ ls -l /usr/local/opt/python
lrwxr-xr-x 1 jim admin 24 2 Mar 13:45 /usr/local/opt/python -> ../Cellar/python/3.6.4_3
As expected, the Python 3.6 directory does not contain a bin/python2.7:
$ ls /usr/local/Cellar/python/3.6.4_3/bin/
2to3 idle pip3 pydoc3.6 python3-config python3.6m-config wheel3
2to3-3.6 idle3 pip3.6 python python3.6 pyvenv
easy_install idle3.6 pydoc python-config python3.6-config pyvenv-3.6
easy_install-3.6 pip pydoc3 python3 python3.6m wheel
virtualenv clearly expects /usr/local/opt/python to contain Python 2 material, but it only contains Python 3 material.
My /usr/local/opt/python is managed by Homebrew. I don't know the provenance of my virtualenv. How do I find out where my /usr/local/bin/virtualenv came from?
Which is to blame? My virtualenv or Homebrew?
The blame for this lies with pip, not Homebrew. My /usr/local/bin/virtualenv came from pip install virtualenv, which embeds an absolute link to the Python interpreter at installation time! I have opened an issue about this unidiomatic behavior.
Same problem on my Mac. Maybe it got broken when I updated to Mojave? Who knows.
Resolved with a brew install of Python 2:
brew install python2
This now takes over from my factory-installed Python 2.7 and gives me a new virtualenv that works:
$ which virtualenv
/usr/local/bin/virtualenv
Firstly, sorry for adding a separate comment here -- I lack the reputation to add a comment to @jameshfisher's answer.
I used homebrew to update python2 on macos to the latest version:
~ ❯❯❯ python2 --version
Python 2.7.15
Which creates/updates the python2 symlink in /usr/local/bin to link to that particular brew installed update:
~ ❯❯❯ ls -ahl =python2
lrwxr-xr-x 1 michael admin 39B 3 Jul 17:11 /usr/local/bin/python2 -> ../Cellar/python@2/2.7.15_1/bin/python2
The shebang in my /usr/local/bin/virtualenv was:
~ ❯❯❯ head -1 $(which virtualenv)
#!/usr/local/opt/python/bin/python2.7
Which did not exist:
~ ❯❯❯ ls -l /usr/local/opt/python/bin/python2.7
ls: /usr/local/opt/python/bin/python2.7: No such file or directory
So modifying the shebang to #!/usr/local/bin/python2 to use the brew installed updated python2 version was the most appropriate way to go:
~ ❯❯❯ virtualenv --version
15.1.0
✨🐟✨
A bit of a red herring for me was that I had mistakenly assumed typing which python would give me the path to my version of python2:
~ ❯❯❯ which python
/Users/michael/.pyenv/shims/python
~ ❯❯❯ which python2
/usr/local/bin/python2
~ ❯❯❯ which python3
/Users/michael/.pyenv/shims/python3
~ ❯❯❯ /Users/michael/.pyenv/shims/python --version
Python 3.7.0
I had forgotten that I had set pyenv global to python 3.7.0. Please don't fall into that trap as I did! ✨😄✨