macOS上的virtualenv使用不存在的python解释器

时间:2018-03-02 14:26:42

标签: python macos virtualenv homebrew

当我运行$ virtualenv -bash: /usr/local/bin/virtualenv: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory 时,我明白了:

virtualenv

virtualenv今天才开始以这种方式行事。它昨天工作。它因为$ head -1 $(which virtualenv) #!/usr/local/opt/python/bin/python2.7 是使用不存在的Python解释器的Python脚本而中断:

/usr/local/opt/python

在我的机器上,$ 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 是Python 3.6目录的符号链接:

bin/python2.7

正如所料,Python 3.6目录不包含$ 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

/usr/local/opt/python显然希望/usr/local/opt/python包含Python 2材质,但它只包含Python 3材质。

我的virtualenv由Homebrew管理。我不知道我/usr/local/bin/virtualenv的出处。如何找到virtualenv来自哪里?

哪个是罪魁祸首?我的 "scripts": { "lint-staged": "$(yarn bin)/lint-staged" }, "lint-staged": { ... "app/assets/styles/**/*.sass": [ "prettier --write", "stylelint --fix --syntax sugarss", "git add" ] } 还是Homebrew?

3 个答案:

答案 0 :(得分:7)

对此的责任在于pip,而不是Homebrew。我的/usr/local/bin/virtualenv来自pip install virtualenv,它在安装时嵌入了Python解释器的绝对链接!我打开了an issue about this unidiomatic behavior

答案 1 :(得分:0)

首先,很抱歉在此处添加单独的评论-我缺乏在@jameshfisher的答案中添加评论的声誉。

我使用自制软件将macOS上的python2更新到最新版本:

~ ❯❯❯ python2 --version
Python 2.7.15

python2中创建/更新/usr/local/bin符号链接以链接到该特定的brew安装的更新:

~ ❯❯❯ ls -ahl =python2
lrwxr-xr-x  1 michael  admin    39B  3 Jul 17:11 /usr/local/bin/python2 -> ../Cellar/python@2/2.7.15_1/bin/python2

我的/usr/local/bin/virtualenv中的shebang是:

~ ❯❯❯ head -1 $(which virtualenv)
#!/usr/local/opt/python/bin/python2.7

哪个不存在:

~ ❯❯❯ ls -l /usr/local/opt/python/bin/python2.7
ls: /usr/local/opt/python/bin/python2.7: No such file or directory

因此,将shebang修改为#!/usr/local/bin/python2以使用brew安装的更新的python2版本是最合适的方法:

~ ❯❯❯ virtualenv --version
15.1.0

✨✨

对我来说有点麻烦,是我错误地认为键入which python会为我提供 python2 版本的路径:

~ ❯❯❯ which python
/Users/michael/.pyenv/shims/python
~ ❯❯❯ which python2
/usr/local/bin/python2
~ ❯❯❯ which python3
/Users/michael/.pyenv/shims/python3

~ ❯❯❯ /Users/michael/.pyenv/shims/python --version
Python 3.7.0

我忘记了将pyenv global设置为python 3.7.0。请不要像我那样陷入陷阱! ✨✨

答案 2 :(得分:0)

我的Mac上存在相同的问题。当我更新到Mojave时,它可能坏了?谁知道。

通过brew安装的Python 2解决了:

brew install python2

现在,它接管了我在工厂安装的Python 2.7,并为我提供了一个可以工作的新virtualenv

$ which virtualenv
/usr/local/bin/virtualenv