41

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?

7 Answers 7

74

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

4
9

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...

6

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!!

1
  • yes - thank you - but also to switch the default python path from 3.7 -> 3.9. You put me on the right track though. (all after High Sierra -> Big Sur upgrade) Thanks!
    – jtlz2
    Commented Aug 12, 2021 at 8:30
3

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
2

For those having similar issues with built-in venv and arriving to the post:

  • the problem could appear when creating a project and .venv folder, and later moving the project to another folder.
  • the simplest solution is to delete and create a new venv.
  • otherwise, go to .venv/bin/activate file to change the $VIRTUAL_ENV to the new path and do the same in the .venv/bin/pip first line (the shebang) and the same with pip3 and pip3.X files in the same folder.
1

In my case, the error shows in Github action and the root cause is the broken python symbolic link. As a workaround, I just simply re-create it during the process.

Error:
home/runner/work/_temp/810926a2-36c5-4488-ac84-6f3f57713147.sh: /home/runner/work/dataops/dataops/.venv/bin/pytest: /home/runner/work/dataops/dataops/.venv/bin/python: bad interpreter: No such file or directory

Root Cause:
/home/runner/work/dataops/dataops/.venv/bin/python: broken symbolic link to /opt/hostedtoolcache/Python/3.9.15/x64/bin/python3.9

Solution:
Add below scripts into wherever has broken link:
        PYTHON_PATH=$(which python3)
        source .venv/bin/activate
        PYTHON_BROKEN_PATH=$(dirname $(which pip))
        rm $PYTHON_BROKEN_PATH/python
        ln -s $PYTHON_PATH $PYTHON_BROKEN_PATH/python
        export PATH=$PATH:$(dirname $PYTHON_BROKEN_PATH)
0

Similar problem after switching package managers on a Mac. No reinstallation required for my use case, just updates to 2 files.

1st update config file

I updated pyvenv.cfg in the virtual environment directory (cd /to/your/venv/dir)

I had to update the home and the version settings.

home = /usr/local/bin
include-system-site-packages = false
version = 3.10.8

2nd fix symlinks

Lastly I updated the symlink to the python executable. It's in the virtual environment's bin directory.

cd /to/your/venv/dir/bin
ln -s /usr/local/bin/python3.10 python

Note, in my case there were 2 other symlinks to python executables in the venv/bin directory, python3 and python3.10. I didn't need to touch these as they were both pointers to the python symlink that was just updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.