virtualenv错误错误的解释器:没有这样的文件或目录

时间:2018-12-20 06:28:08

标签: python virtualenv

如果我尝试创建虚拟环境,则会收到此错误消息,我不理解该错误消息:错误的解释器:没有此类文件或目录。我已经审查了this stack overflow answer,并尝试将其应用于以下诊断步骤。

这是我在Mojave上跑步的第一天,但​​我不知道这是否是造成此问题的原因。

我在/Users/Wes/Dropbox/Programming/Python/glade_againn为项目创建了一个新的空文件夹

我的计划是在virtualenv /Users/Wes/.virtualenvs/glade_againn

中运行项目

但是,当我尝试使用virtualenv时,会出现此错误消息。

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

如果我尝试通过PIP安装virtualenv,则会被告知已经存在。

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

我当前的PATH是

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/

如果您在所有这些目录中搜索pyth *,则会按此顺序获得此列表。

/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

有人对如何使virtualenv重新工作提出建议吗?

4 个答案:

答案 0 :(得分:4)

就我而言,我正在重命名项目和项目的文件夹,其中 venv 所在的位置。

所以就我而言,我在以下文件中更改了 python 解释器的路径:

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

并将 ~/PycharmProjects/myproject/venv/bin/pip* 文件修改为:

#!/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())

要修改它,我需要以 root 权限登录:sudo susudo vim.tiny venv/bin/pip 只是不允许我编辑文件。

我只更改了以 #!/home... 开头的第一行

答案 1 :(得分:2)

尝试使用此方法重新安装

pip install -U --force-reinstall virtualenv

如果上述解决方案对您不起作用 您应该由于莫哈韦沙漠更新再次创建一个新的virtualenv

答案 2 :(得分:2)

就我而言,我使用的是 MacOS 并且安装了 python3.9,但是 virtualenv 是使用 python3.7 安装的,并且在某些时候我卸载了 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

但是,我的python版本:

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

没有多少 pip 或 pip3 install/uninstall/install virtualenv 对我有用。 最后我做了以下事情:

$ 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

然后

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

耶!!

答案 3 :(得分:0)

这解决了我的问题:(我的 env 文件名为 .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