在Mac OS X上的python位置

问题:在Mac OS X上的python位置

我对osx上的python感到有些困惑。我不知道笔记本电脑的先前所有者是否已使用macport安装了macpython。我记得osx具有内置版本的python。我尝试使用type -a python,结果返回

python is /usr/bin/python
python is /usr/local/bin/python

但是在这些位置运行两个python给了我[GCC 4.2.1 (Apple Inc. build 5646)] on darwin。它们是否都引用了提供的相同内置python mac?

我还读到安装macpython会

     A MacPython 2.5 folder in your Applications folder. In here you
 find IDLE, the development environment that is a standard part of
 official Python distributions...

我看了看Applications,里面有一个MacPort文件夹,上面python2.6有提到的东西。但是运行IDLE,我发现与上面相同的消息。

嗯,我有点困惑。哪一个

I’m a little confused with the python on osx. I do not know if the previous owner of the laptop has installed macpython using macport. And I remembered that osx has an builtin version of python. I tried using type -a python and the result returned

python is /usr/bin/python
python is /usr/local/bin/python

However running both python at these locations give me [GCC 4.2.1 (Apple Inc. build 5646)] on darwin. Do they both refer to the same builtin python mac provided?

I also read that installing macpython one would

     A MacPython 2.5 folder in your Applications folder. In here you
 find IDLE, the development environment that is a standard part of
 official Python distributions...

I looked at Applications, and theres a MacPort folder with python2.6 and the mentioned stuff in it. But running IDLE, i find the same message as above.

Hmm I’m a little confused. Which is which?


回答 0

[GCC 4.2.1 (Apple Inc. build 5646)]是构建Python的GCC版本,而不是Python本身的版本。该信息应在前一行。例如:

# Apple-supplied Python 2.6 in OS X 10.6
$ /usr/bin/python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

# python.org Python 2.7.2 (also built with newer gcc)
$ /usr/local/bin/python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

/usr/bin除非某人不明智地更改了其中的内容,否则其中的项目应该始终是或链接到Apple在OS X中提供的文件。要确切了解/usr/local/bin/python链接到的位置:

$ ls -l /usr/local/bin/python
lrwxr-xr-x  1 root  wheel  68 Jul  5 10:05 /usr/local/bin/python@ -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python

在这种情况下,这对于python.org安装的Python实例来说是典型的,也可以是从源代码构建的。

[GCC 4.2.1 (Apple Inc. build 5646)] is the version of GCC that the Python(s) were built with, not the version of Python itself. That information should be on the previous line. For example:

# Apple-supplied Python 2.6 in OS X 10.6
$ /usr/bin/python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

# python.org Python 2.7.2 (also built with newer gcc)
$ /usr/local/bin/python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Items in /usr/bin should always be or link to files supplied by Apple in OS X, unless someone has been ill-advisedly changing things there. To see exactly where the /usr/local/bin/python is linked to:

$ ls -l /usr/local/bin/python
lrwxr-xr-x  1 root  wheel  68 Jul  5 10:05 /usr/local/bin/python@ -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python

In this case, that is typical for a python.org installed Python instance or it could be one built from source.


回答 1

在Mac OS X上,它位于的Python框架中/System/Library/Frameworks/Python.framework/Resources

完整路径为:

/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python

顺便说一句,很容易找到在哪里可以找到特定的二进制文件:which Python将向您显示Python二进制文件的路径(可能与我上面发布的相同)。

On Mac OS X, it’s in the Python framework in /System/Library/Frameworks/Python.framework/Resources.

Full path is:

/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python

Btw it’s easy to find out where you can find a specific binary: which Python will show you the path of your Python binary (which is probably the same as I posted above).


回答 2

我找到了最简单的定位方法,可以使用

which python

它会显示如下内容:

/usr/bin/python

I found the easiest way to locate it, you can use

which python

it will show something like this:

/usr/bin/python


回答 3

这个会 解决您所有的问题使用Python和Mac时:

如果您有Mac,并且像我们大多数人一样安装了python3:)(使用brew install-ofc)

您的文件位于:

/usr/local/Cellar/python/3.6.4_4/bin/python3

你怎么知道的?跑:

which python3

您应该得到:

/usr/local/bin/python3

现在这是一个符号链接,您怎么知道?跑:

ls -al /usr/local/bin/python3 

您会得到:

/usr/local/bin/python3 -> /usr/local/Cellar/python/3.6.4_4/bin/python3

这意味着你

/usr/local/bin/python3 

实际上指向:

/usr/local/Cellar/python/3.6.4_4/bin/python3

如果由于某种原因

/usr/local/bin/python3 

没有指向您想要的地方,在我们的例子中:

/usr/local/Cellar/python/3.6.4_4/bin/python3

只需备份:

cp /usr/local/bin/python3{,.orig} 

并运行:

rm -rf /usr/local/bin/python3

现在创建一个新的符号链接:

ln -s /usr/local/Cellar/python/3.6.4_4/bin/python3 /usr/local/bin/python3 

现在你的

/usr/local/bin/python3

指向

/usr/local/Cellar/python/3.6.4_4/bin/python3 

通过运行以下命令进行检查:

ls -al /usr/local/bin/python3

This one will solve all your problems dealing with Python on basic shell:

If you have a Mac and you’ve installed python3 like most of us do :) (with brew install – ofc)

your file is located in:

/usr/local/Cellar/python/3.6.4_4/bin/python3

How do you know? -> you can run this on every basic shell Run:

which python3

You should get:

/usr/local/bin/python3

Now this is a symbolic link, how do you know? Run:

ls -al /usr/local/bin/python3 

and you’ll get:

/usr/local/bin/python3 -> /usr/local/Cellar/python/3.6.4_4/bin/python3

which means that your

/usr/local/bin/python3 

is actually pointing to:

/usr/local/Cellar/python/3.6.4_4/bin/python3

If, for some reason, your

/usr/local/bin/python3 

is not pointing to the place you want, which in our case:

/usr/local/Cellar/python/3.6.4_4/bin/python3

just backup it:

cp /usr/local/bin/python3{,.orig} 

and run:

rm -rf /usr/local/bin/python3

now create a new symbolic link:

ln -s /usr/local/Cellar/python/3.6.4_4/bin/python3 /usr/local/bin/python3 

and now your

/usr/local/bin/python3

is pointing to

/usr/local/Cellar/python/3.6.4_4/bin/python3 

Check it out by running:

ls -al /usr/local/bin/python3

回答 4

在高山脉

which python

显示默认的python,但是如果您从python.org下载并安装了最新版本,则可以通过以下方式找到它:

which python3.6

我机器上显示的

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6

On High Sierra

which python

shows the default python but if you downloaded and installed the latest version from python.org you can find it by:

which python3.6

which on my machine shows

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6

回答 5

用“ brew install python3”安装,在这里找到

installed with ‘brew install python3’, found it here


回答 6

我检查了一些类似的讨论,发现找到所有python2 / python3构建的最佳方法是:

which -a python python3

I checked a few similar discussions and found out the best way to locate all python2/python3 builds is:

which -a python python3

回答 7

我在这里找到它:/Library/Frameworks/Python.framework/Versions/3.6/bin

i found it here: /Library/Frameworks/Python.framework/Versions/3.6/bin


回答 8

which python3 只会导致解释者在其中安定下来。

which python3 simply result in a path in which the interpreter settles down.


回答 9

在您的交互式终端中运行

import os
os.path

它将为您提供安装python的文件夹

Run this in your interactive terminal

import os
os.path

It will give you the folder where python is installed


回答 10

在.py文件中运行以下代码:

import sys

print(sys.version)
print(sys.executable)

run the following code in a .py file:

import sys

print(sys.version)
print(sys.executable)

回答 11

我有一个在Linux / macos中寻找东西的厨师食谱

首先更新定位数据库,然后执行

locate WHATiWANTtoSEARCH | less

/找到您想要的东西。

要在macOS中更新您的定位数据库,请执行以下操作:

sudo /usr/libexec/locate.updatedb

有时需要一段时间。希望这可以帮助 :)

I have a cook recipe for finding things in linux/macos

First update the locate db then do a

locate WHATiWANTtoSEARCH | less

do a /find to find what you are looking for.

to update your locate db in macos do this:

sudo /usr/libexec/locate.updatedb

it sometimes takes a while. Hope this helps :)