python virtualenv错误错误的解释程序:无此文件或目录

gwbalxhn  于 2022-12-17  发布在  Python
关注(0)|答案(5)|浏览(190)

如果我尝试创建一个虚拟环境,我得到这个错误消息,我不明白:错误的解释程序:没有这样的文件或目录。我已经查看了this stack overflow answer,并尝试在下面的诊断步骤中应用它。
这是我第一天在莫哈韦跑步,但我不知道这是否是这个问题的一个因素。
我已在/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)
$

我当前的路径是

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/

如果您在所有这些目录中搜索python *,您会得到这个列表,顺序如下。

/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重新工作有什么建议吗?

qjp7pelc

qjp7pelc1#

尝试使用此重新安装

pip install -U --force-reinstall virtualenv

如果以上解决方案不适合您,您应该因为莫哈韦更新而再次创建一个新virtualenv

kr98yfug

kr98yfug2#

在我的例子中,我正在重命名项目和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...开头的第一行

6bc51xsx

6bc51xsx3#

在我的例子中,我在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或pip 3安装/卸载/安装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>

耶!

eni9jsuy

eni9jsuy4#

这就解决了我的问题:(我的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
332nm8kg

332nm8kg5#

在我的例子中,错误显示在Github操作中,根本原因是损坏的python符号链接。作为一种解决方案,我只是在过程中简单地重新创建它。

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)

相关问题