virtualenv error bad interpreter: No such file or directory

Viewed 35280

If I attempt to create a virtual env I get this error message, which I do not understand: bad interpreter: No such file or directory. I have reviewed this stack overflow answer and have tried to apply it in the diagnostic steps below.

This is my first day running on Mojave but I don't know if that is a factor in this issue or not.

I have created a new empty folder for the project at /Users/Wes/Dropbox/Programming/Python/glade_againn

My plan has been to run the project in the virtualenv /Users/Wes/.virtualenvs/glade_againn

However, when I attempt to use virtualenv I get this error message.

$ virtualenv --version
-bash: /usr/local/bin/virtualenv: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory

If I attempt to install virtualenv with PIP I am told it already exists.

$ pip install virtualenv
Requirement already satisfied: virtualenv in /usr/local/lib/python2.7/site-packages (15.2.0)
$ 

My current PATH is

echo $PATH
/Library/Frameworks/Python.framework/Versions/3.6/bin:/opt/local/bin:/opt/local/sbin:/usr/local/opt/postgresql@9.4/bin:/usr/local/Cellar/postgresql/9.5.4_1/bin/psql/:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Applications/Wireshark.app/Contents/MacOS:/usr/local/git/bin:/Users/Wes/bin:/sw/bin:/usr/local/bin:/Users/Wes/.sdkman/candidates/groovy/current/bin/

If you search for pyth* across all those directories you get this list, in this order.

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3-config
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6-config
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m-config
/opt/local/bin/python2.7
/opt/local/bin/python2.7-config
/opt/local/bin/python3.4
/opt/local/bin/python3.4-config
/opt/local/bin/python3.4m
/opt/local/bin/python3.4m-config
/opt/local/bin/pythonw2.7
/usr/local/bin/python-32
/usr/local/bin/python2-32
/usr/local/bin/python2.7-32
/usr/local/bin/python3
/usr/local/bin/python3-config
/usr/local/bin/python3.6
/usr/local/bin/python3.6-config
/usr/local/bin/python3.6m
/usr/local/bin/python3.6m-config
/usr/local/bin/pythoni
/usr/local/bin/pythoni1
/usr/local/bin/pythonw-32
/usr/local/bin/pythonw2-32
/usr/local/bin/pythonw2.7-32
/usr/bin/python
/usr/bin/python-config
/usr/bin/python2.7
/usr/bin/python2.7-config
/usr/bin/pythonw
/usr/bin/pythonw2.7
/sw/bin/python2.7
/sw/bin/python2.7-config
/usr/local/bin/python-32
/usr/local/bin/python2-32
/usr/local/bin/python2.7-32
/usr/local/bin/python3
/usr/local/bin/python3-config
/usr/local/bin/python3.6
/usr/local/bin/python3.6-config
/usr/local/bin/python3.6m
/usr/local/bin/python3.6m-config
/usr/local/bin/pythoni
/usr/local/bin/pythoni1
/usr/local/bin/pythonw-32
/usr/local/bin/pythonw2-32
/usr/local/bin/pythonw2.7-32

Does anyone have a suggestion on how to get virtualenv to work again?

4 Answers

Try to reinstall using this

pip install -U --force-reinstall virtualenv

if above solution doesn't work for you you should create a new virtualenv again because of mojave update

In my case I was renaming project and project's folder where venv has been located.

So in my case I was changing paths to python interpreter in the following files:

~/PycharmProjects/myproject/venv/bin/activate*

And modified ~/PycharmProjects/myproject/venv/bin/pip* files to:

#!/home/myuser/PycharmProjects/myproject/venv/bin/python
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal.cli.main import main
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

To modify it I was required to login with root permissions: sudo su. The sudo vim.tiny venv/bin/pip just not allowed me to edit the files.

I've changed only the first line starting with #!/home...

In my case, I was on MacOS and I had python3.9 installed, but virtualenv was installed using python3.7 and at some point I uninstalled python3.7.

$ /usr/local/bin/virtualenv --version
-bash: /usr/local/bin/virtualenv: /usr/local/opt/python/bin/python3.7: bad interpreter: No such file or directory

However, my python version:

$ which python3.9
/usr/local/bin/python3.9

No amount of pip or pip3 install/uninstall/install virtualenv worked for me. Finally I did the following:

$ python3.9 -m pip install --user virtualenv
Collecting virtualenv
  Using cached virtualenv-20.4.6-py2.py3-none-any.whl (7.2 MB)
<snip>
Successfully installed appdirs-1.4.4 distlib-0.3.1 filelock-3.0.12 virtualenv-20.4.6

And then

$ /usr/local/bin/virtualenv --version
virtualenv 20.4.6 from <mypath>

Yay!!

That solved the problem in my case: (my env file is called .venv)

mv .venv .venv_old
python3.7 -m venv .venv
source .venv/bin/activate
pip install wheel
pip install --upgrade pip wheel setuptools
pip install -r requirements.txt
Related