1

tldr: How is Python set up on a Mac? Is there a ton of senseless copying going on even before I start wrecking it?

I am hoping to get some guidance regarding Python system architecture on Mac (perhaps the answer is OS agnostic, but I assume for safety's sake that it is not).

I can run a variety of commands that seem to give me multiple Python binaries. In truth, there may be more this is just what I have come across so far.

  1. ls /usr/local/bin/ | grep 'python\|pyd'

pydoc pydoc2 pydoc2.7 python python-32 python-config python2 python2-32 python2-config python2.7 python2.7-32 python2.7-config pythonw pythonw-32 pythonw2 pythonw2-32 pythonw2.7 pythonw2.7-32

  1. ls /usr/bin | grep 'python\|pyd'

pydoc pydoc2.6 pydoc2.7 python python-config python2.6 python2.6-config python2.7 python2.7-config pythonw pythonw2.6 pythonw2.7

  1. ls /Library/Frameworks/Python.framework/Versions/

2.7 Current

  1. ls /System/Library/Frameworks/Python.framework/Versions/

2.3 2.5 2.6 2.7 Current

As far as which one runs when executing a .py; when I run which python I get back

/Library/Frameworks/Python.framework/Versions/2.7/bin/python

This seems consistent when I use the REPL. The site-packages relative to this install are available (not that I tinkered with other site package locs)

I have not made any serious modifications to my python environment on my Mac so I am assuming this is what is given to users out of the box. If anyone understands how all these binaries fit together and why they all exist please let me know. If the answer is RTM please simply point me to a page as https://docs.python.org/2/using/mac.html did not suffice.

Thanks for making me smarter!

SPECS: Mac OS: 10.12.5

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
mattc41190
  • 31
  • 6
  • What exactly is your question? Do you want to know what each and every one of those commands does? – jwodder Jul 31 '17 at 20:53
  • My recipe: (1) Always use `brew install python`, never use the system Python (as in `/usr/bin/python*`) for dev work. (2) Always create a virtualenv, never `pip install` stuff into the interpreter's environment. On my machine, I don't see the proliferation of the Python interpreters you refer to. Having multiple interpreters is useful for testing, but see above. – 9000 Jul 31 '17 at 21:01
  • 1
    @jwodder I want to know 1.) if you have the same setup wherein (on a "pristine" machine) there are multiple locations for python binaries. 2.) If so, why and are they used in some sort of orchestration, or are they truly just copies? I know it can be a lot, if you know the portion of the docs that explains their arch on each OS I would be thankful. – mattc41190 Jul 31 '17 at 21:27
  • @9000 glad to know it may be my own silliness in wrecking my machine. Which of the paths that I posted renders a "No such file or directory"? I totally agree with regarding the virtualization of envs and as it seems my dev work may be more python geared in the future I will likely heed your advice. – mattc41190 Jul 31 '17 at 21:30

2 Answers2

0

Disclaimer: I know precious little about modern macOS.

All files under /System/Library/Frameworks/Python.framework/Versions/2.*/ seem to be the same files. That is, the below shows 3 identical checksums:

md5 /System/Library/Frameworks/Python.framework/Versions/2.{3,5,6}/bin/python

I suppose it's for macOS applications that assume a particular early Python version available. They all are given the same 2.6.9 which is the last version of 2.6. The 2.x/lib all contain python2.6 subdirectory with .py* files, that also seem identical:

md5 /System/Library/Frameworks/Python.framework/Versions/2.{3,5,6}/lib/python2.6/threading.pyc

I don't know if all these files are hardlinks to the same bytes, but it seems likely. All interpreters seem the same:

$ find /System/Library/Frameworks/Python.framework/Versions/2.{3,5,6}/bin/* \
  -samefile /System/Library/Frameworks/Python.framework/Versions/2.6/bin/python

/System/Library/Frameworks/Python.framework/Versions/2.3/bin/python2.6
/System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.6
/System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6

Python 2.7 is the only maintained 2.x version, so it's separate and likely receives updates.

9000
  • 39,899
  • 9
  • 66
  • 104
0

To answer your broader question about why there are various different executables for Python,

  • python is the basic command-line interface Python executable.
  • pythonw exists to run 'headless' Python programs, without provision terminal output. This distinct executable is necessary in particular on Windows for GUI-based Python programs where no console window should be opened, hence the name.
  • python-config scripts exist only to provide configuration information to programs seeking to compile against or link to a particular Python binary, eg when creating Python extension modules (.pyd files, I believe, on OSX)
  • pydoc extracts documentation information from Python script files, eg from docstrings association with specific classes and functions.

Some more on the python versus pythonw difference: https://stackoverflow.com/a/30313091/1234300

Apart from that, you have some versions specifically labelled python-32 etc. Those will be 32-bit versions, compared to 64-bit which is probably what the other versions are on your system. Sometimes you may want to use a 32-bit version of Python because you want to link to other 32-bit code, including Python extensions or other shared libraries.

jdpipe
  • 232
  • 1
  • 11
  • Awesome answer! Thank you! – mattc41190 Aug 01 '17 at 14:10
  • Any clue as to why they are in both usr/bin and usr/local/bin? – mattc41190 Aug 01 '17 at 14:11
  • Paraphrasing my understanding of POSIX standards, /usr/bin should be for executables that come as part of the operating system (or packages managed by the operating system), and /usr/local/bin should be for executables installed locally on your specific machine, managed by you as the sysadmin for that machine. If you have *both* of these, it suggests you might have two (or more!) copies of Python installed; in that case be very careful that you know which one you are using, and whether that's what you want. – jdpipe Aug 02 '17 at 00:10