Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

virtualenv on macOS uses nonexistent python interpreter

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?

like image 836
jameshfisher Avatar asked Mar 02 '18 14:03

jameshfisher


2 Answers

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
like image 82
Robert Avatar answered Sep 29 '22 22:09

Robert


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.

like image 40
jameshfisher Avatar answered Sep 29 '22 22:09

jameshfisher