OSX:使用bash发现蟒蛇是不是使用的/ usr / bin中不同/ env的蟒蛇 [英] OSX: using bash to find python is different than using /usr/bin/env python

查看:166
本文介绍了OSX:使用bash发现蟒蛇是不是使用的/ usr / bin中不同/ env的蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道做这个东西。如果我问我的shell(bash)的推出蟒蛇没有给它一个确切的路径,它在某种程度上会启动错误的程序(也可能是正确的程序,但用错 dylib 取值加载)。然而,的Python 的输出在/ usr /斌/包膜蟒蛇都蛮好看的。

下面就是我在谈论的一个例证:

确定,首先,这里是我的蟒蛇命令来自何处?让我们跟随符号链接。 (顺便说一句,太糟糕了的readlink -f 的真实路径不要在OSX存在...)

$其中蟒蛇
在/ usr / local / bin目录/蟒蛇
$ ls -l命令在/ usr / local / bin目录/蟒蛇
lrwxr-XR-X 1根轮68月18日11:44在/ usr / local / bin目录/蟒蛇@ - > ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ ls -l命令/Library/Frameworks/Python.framework/Versions/2.7/bin/python
lrwxr-XR-X 1根管理18年1月7日11:44 /Library/Frameworks/Python.framework/Versions/2.7/bin/python@ - > python2
$ ls -l命令/Library/Frameworks/Python.framework/Versions/2.7/bin/python2
lrwxr-XR-X 1根管理18年1月9日11:44 /Library/Frameworks/Python.framework/Versions/2.7/bin/python2@ - > python2.7
$ ls -l命令/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
-rwxrwxr-X 1根admin 25624 12月5日15:57 /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7*

如果我称之为的/ usr / local / bin目录/蟒蛇,哪里蟒蛇本身认为这是在什么位置?什么版本呢?

$在/ usr / local / bin目录/蟒蛇-c进口SYS;打印sys.executable;打印sys.version_info
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
sys.version_info(主要= 2,次要= 7,微型= 11,releaselevel =最终,序列= 0)

OK,看起来合法的。无需键入的/ usr / local / bin目录/蟒蛇,如果我让的/ usr / bin中/ env的定位蟒蛇我吗?

$的/ usr /斌/包膜蟒蛇-c进口SYS;打印sys.executable;打印sys.version_info
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
sys.version_info(主要= 2,次要= 7,微型= 11,releaselevel =最终,序列= 0)

同样的事情。没有惊喜至今。好吧,如果我只需要输入什么蟒蛇?这应该是一样的的/ usr /斌/包膜蟒蛇,对吧?

$蟒蛇-c进口SYS;打印sys.executable;打印sys.version_info
在/ usr / local / bin目录/蟒蛇
sys.version_info(主要= 2,次要= 7,微型= 10,releaselevel =最终,序列= 0)

等等,什么? sys.executable 指向符号链接,然后版本是错误的。这是怎么回事?

为什么会打字蟒蛇以任何方式不同输入的/ usr / local / bin目录/蟒蛇 <? / p>

BTW:


  • 我双重检查我的bash的别名,而蟒蛇是不是其中之一。 (别名| grep的蟒蛇


解决方案

Bash保留缓存查找表在内存中,您可以用审视。如果将程序添加到您的 PATH 具有相同的名称与您在不同的位置之前使用的程序,庆典仍然会记得原来的位置。 ENV 显然绕过这个缓存;或者你可以用散-r蟒蛇

删除

另外请注意,不可移植,通常不使用正确的工具; 键入由POSIX指定的,并且是内置在任何现代的外壳。

I don't know what to make of this. If I ask my shell (bash) to launch python without giving it an exact path, it somehow launches the wrong program (or possibly the right program, but with the wrong dylibs loaded). Yet, the output of which python and /usr/bin/env python both look just fine.

Here's an illustration of what I'm talking about:

OK, first of all, where is my python command coming from? Let's follow the symlinks. (BTW, Too bad readlink -f and realpath don't exist on OSX...)

$ which python
/usr/local/bin/python
$ ls -l /usr/local/bin/python
lrwxr-xr-x  1 root  wheel  68 Jan 18 11:44 /usr/local/bin/python@ -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ ls -l /Library/Frameworks/Python.framework/Versions/2.7/bin/python
lrwxr-xr-x  1 root  admin  7 Jan 18 11:44 /Library/Frameworks/Python.framework/Versions/2.7/bin/python@ -> python2
$ ls -l /Library/Frameworks/Python.framework/Versions/2.7/bin/python2
lrwxr-xr-x  1 root  admin  9 Jan 18 11:44 /Library/Frameworks/Python.framework/Versions/2.7/bin/python2@ -> python2.7
$ ls -l /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
-rwxrwxr-x  1 root  admin  25624 Dec  5 15:57 /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7*

If I call /usr/local/bin/python, where does python itself think it is located? And what version is it?

$ /usr/local/bin/python -c "import sys; print sys.executable; print sys.version_info"
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
sys.version_info(major=2, minor=7, micro=11, releaselevel='final', serial=0)

OK, looks legit. Instead of typing /usr/local/bin/python, what if I let /usr/bin/env locate python for me?

$ /usr/bin/env python -c "import sys; print sys.executable; print sys.version_info"
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
sys.version_info(major=2, minor=7, micro=11, releaselevel='final', serial=0)

Same thing. No surprises so far. OK, what if I just type python? That should be the same as /usr/bin/env python, right?

$ python -c "import sys; print sys.executable; print sys.version_info"
/usr/local/bin/python
sys.version_info(major=2, minor=7, micro=10, releaselevel='final', serial=0)

Wait, what? sys.executable points to the symlink, and the version is wrong. What's going on?

Why would typing python be different in any way from typing /usr/local/bin/python?

BTW:

  • I double-checked my bash aliases, and python is not one of them. (alias | grep python)

解决方案

Bash keeps a cached lookup table in memory, which you can examine with hash. If you add a program to your PATH with the same name as a program you have used before at a different location, bash will still remember the old location. env obviously bypasses this cache; or you can remove it with hash -r python.

Also note that which is nonportable and often not the correct tool to use; type is specified by POSIX, and is built-in to any modern shell.

这篇关于OSX:使用bash发现蟒蛇是不是使用的/ usr / bin中不同/ env的蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆