[英]Why and how: Missing symlink for python3?
I am on macOS 10.15.7.我在 macOS 10.15.7 上。
I need both python2
and python3
.我需要
python2
和python3
。 I am a beginner which is why I often struggle to understand many terms.我是初学者,这就是为什么我经常难以理解许多术语的原因。 A fellow student told me that the
->
are symlinks.一位同学告诉我
->
是符号链接。
When running me$ which python3
I get:运行
me$ which python3
时,我得到:
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
But when checking ll /usr/bin/python*
但是当检查
ll /usr/bin/python*
lrwxr-xr-x 1 root wheel 75B Oct 9 2019 /usr/bin/python -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x 1 root wheel 82B Oct 9 2019 /usr/bin/python-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
lrwxr-xr-x 1 root wheel 75B Oct 9 2019 /usr/bin/python2 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x 1 root wheel 75B Oct 9 2019 /usr/bin/python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x 1 root wheel 82B Oct 9 2019 /usr/bin/python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
-rwxr-xr-x 1 root wheel 31K Oct 30 2020 /usr/bin/python3
lrwxr-xr-x 1 root wheel 76B Oct 9 2019 /usr/bin/pythonw -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
lrwxr-xr-x 1 root wheel 76B Oct 9 2019 /usr/bin/pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
Everything looks the same except for the line concerning python3.除了关于 python3 的那一行之外,一切看起来都一样。
which python3
return a different path than the one I see when ll
ing /usr/bin/python*
.which python3
返回的路径与ll
在输入/usr/bin/python*
时看到的路径不同。 I mean both exist... So, why is /Library/Frameworks/Python.framework/Versions/3.9/bin/python3
preferred?/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
是首选?manually
generate a symlink between the /usr/bin/python3
and the /Library/Frameworks/Python.framework/Versions/3.9/bin/python3
as shown here ./usr/bin/python3
和/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
之间manually
生成符号链接是否“安全”,如此处所示。 ln -s /path/to/original/ /path/to/link
Being a beginner I'd rather ask the internet first;) ln -s /path/to/original/ /path/to/link
作为初学者我宁愿先上网;) I'm happy to edit my question following your suggestions as I'm here to learn - and hopefully soon also to help.我很高兴根据您的建议编辑我的问题,因为我在这里学习 - 希望很快也能提供帮助。
Thanks for your help.谢谢你的帮助。
There's actually a fair bit of research here.这里实际上有相当多的研究。
The 2.7 Python is the Apple-used Python, best leave it alone. 2.7 Python 是苹果用的 Python,最好别管它。 I would also leave the existing /usr/bin/python3 alone dont replace it with a symlink.
我也会单独留下现有的 /usr/bin/python3 不要用符号链接替换它。 As to results of which, they depend on the contents of the PATH variable.
至于其中的结果,它们取决于 PATH 变量的内容。 wherever something is first found is where it is gotten from.
东西首先被发现的地方就是它的来源。
/Library/Frameworks/Python.framework/Versions/3.9/bin/
must precede /usr/bin
in PATH and they both have a python3
entry. /Library/Frameworks/Python.framework/Versions/3.9/bin/
必须在 PATH 中的/usr/bin
之前,并且它们都有一个python3
条目。
The easiest way to have a stable Python to work with (and no, I don't recommend Python 2.7 as part of that) is to use macport or brew to install Python, which install Python symlinks in a given directory, separate from system directories like /usr/bin
. The easiest way to have a stable Python to work with (and no, I don't recommend Python 2.7 as part of that) is to use macport or brew to install Python, which install Python symlinks in a given directory, separate from system directories像
/usr/bin
。
(but do note chepner's comment about brew, he looks to be on to something and he's probably forgotten more about Python than I ever knew) (但请注意 chepner 关于 brew 的评论,他似乎正在做某事,而且他可能比我所知道的更多地忘记了 Python)
Then use virtualenv or an equivalent to pick the correct Python to use for a given project.然后使用virtualenv或等效项选择正确的 Python 用于给定项目。
I would keep as a general policy leaving the contents of /usr/bin
alone, unless you have some really good reason.作为一般政策,我会保留
/usr/bin
的内容,除非你有一些非常好的理由。
This is a primer on the macos directory structure, with emphasis on PATH.这是关于 macos 目录结构的入门,重点是 PATH。
I you want to see what's in your PATH, you can echo $PATH
or do the following:如果您想查看 PATH 中的内容,您可以
echo $PATH
或执行以下操作:
create file myshellutils.sh
:创建文件
myshellutils.sh
:
paste in the following:粘贴以下内容:
showpath ()
{
IFS=':' read -a array <<< "$PATH";
for index in "${!array[@]}";
do
echo "$index ${array[index]}";
done
}
save it, then type source myshellutils.sh
.保存它,然后输入
source myshellutils.sh
。 That creates a new shell function, showpath
.这将创建一个新的 shell function,
showpath
。
which you can use as follows (some stuff edited out).您可以按如下方式使用它(一些东西被编辑掉了)。
(venv38) me@bin$ showpath
0 /Users/me/.cargo/bin
1 /Users/me/venv38/bin 👈 this is where the VirtualEnv-activated version of Python lives
3 /opt/local/bin 👈 where macports puts things
4 /usr/local/bin 👈 where brew puts things. Yes, I had to use both.
5 /usr/bin 👈 Apple stuff, leave alone.
6 /bin
7 /usr/sbin
8 /sbin
10 /opt/local/sbin
12 /Users/me/bin
Last, but not least... a symlink?最后但并非最不重要的……符号链接? Whuzzat?
胡扎特? A pretty clever idea actually, it's basically a pointer, or a marker if you wish, to another file somewhere else with a potentially different name.
实际上,这是一个非常聪明的想法,它基本上是一个指针,或者如果您愿意,它是一个标记,指向另一个可能具有不同名称的文件。
so, say your programs point to which Python to run因此,假设您的程序指向要运行的 Python
foo.py : foo.py :
#!/usr/bin/env python
print("hello")
so, first let's which -a python
(nice call on -a, L.Grozinger )所以,首先让我们看看
which -a python
(很好地调用 -a, L.Grozinger )
which gets得到
/Users/me/venv38/bin/python
let's cd there, then ls -l python*
with a cut to get rid of all the user and size stuff.让我们 cd 那里,然后
ls -l python*
删除所有用户和大小的东西。
$ls -l python* | cut -c 46-
python -> python3.8 👈 1️⃣ your entry point for where foo.py looks for "python". now look for "python3.8"
python3 -> python3.8
👇 2️⃣ which resolves to where macport installs
python3.8 -> /opt/local/Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8 binaries.
The clever thing?聪明的东西? As you install Python 3.9, it will be linked to by
python -> python3.9
and foo.py
will never care.当您安装 Python 3.9 时,它将通过
python -> python3.9
链接到,而foo.py
将永远不会关心。
From the which
man page从
which
手册页
which returns the pathnames of the files (or links) which would be executed in the current environment, had its arguments been given as commands in a strictly POSIX-conformant shell
它返回将在当前环境中执行的文件(或链接)的路径名,其 arguments 作为严格符合 POSIX 的 shell 中的命令给出
and in the options list并在选项列表中
-a print all matching pathnames of each argument
So, without the -a
, which
simply prints the first 'hit' on your PATH
.因此,如果没有
-a
, which
只会在PATH
上打印第一个“命中”。 So for example, if /Library/Frameworks/Python.framework/Versions/3.9/bin
is before /usr/bin
in your PATH
, which python3
will return only one.例如,如果
/Library/Frameworks/Python.framework/Versions/3.9/bin
在您的PATH
中的/usr/bin
之前, which python3
将只返回一个。
I would be willing to bet that your running which -a python3
will return:我愿意打赌您的运行
which -a python3
将返回:
/Library/Frameworks/Python.framework/Versions/3.9/bin
/usr/bin/python3
Your install went fine, you just have more than one python3
executable installed.你的安装很顺利,你只安装了多个
python3
可执行文件。 There is also no need to do any extra symlinking.也不需要做任何额外的符号链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.